Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union types don't match function signatures #7380

Closed
ghost opened this issue Mar 3, 2016 · 3 comments
Closed

Union types don't match function signatures #7380

ghost opened this issue Mar 3, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@ghost
Copy link

ghost commented Mar 3, 2016

TypeScript Version:

nightly (1.9.0-dev.20160303)

Code

interface Foo {
    foo( key: string ): string;
    foo( keys: string[] ): string[];
}

declare var foo: Foo;
declare const bar: string|string[];

const baz = foo.foo( bar );

Expected behavior:
The code is valid and compiles without warning, because interface Foo contains a signature for a method foo corresponding to each type in the union string|string[]. The foo method can accept a parameter list of string, and it can also accept a parameter list of string[], therefore an argument list of string|string[] is satisfiable.

I would expect the return type for foo.foo invoked in this faction to be the type union of the return types of all matching signatures (so string|string[]). This code produces the same error even if all matching signatures have the same return type.

Actual behavior:
Argument of type 'string | string[]' is not assignable to parameter of type 'string[]' Type 'string' is not assignable to type 'string[]'.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 3, 2016
@RyanCavanaugh
Copy link
Member

Duplicate #1805

@mhegazy
Copy link
Contributor

mhegazy commented Mar 3, 2016

you can also find a long discussion on why { foo(string|string[]):string|string[] } is not the same as { foo (string): string; foo (string[]): string[]; } at #7230. This should help shedding some light on how the API should be authored.

@ghost
Copy link
Author

ghost commented Mar 3, 2016

Maybe this is a degenerate case, but I feel like this instance should be satisfiable.
I feel like defining the interface this way is preferrable, because we can preserve the mapping for string->string and string[]->string[]. Passing in a parameter that matches both should simply result in string|string[]->string|string[]. Being less specific in what is passed in results in a result that is less specific, and vice versa.
In my actual code, both methods return void, so there really is no ambiguity; still, this is unsupported.

My actual code I'm using chrome.storage.StorageArea.remove, which can accept a string or string[]. I have keys which can be string or string[], so it's perfectly valid to pass it in; just the typings disagree with me.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants