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

Overload signature that uses generics breaks as of 2.7.1 #22075

Closed
bjnsn opened this issue Feb 20, 2018 · 4 comments
Closed

Overload signature that uses generics breaks as of 2.7.1 #22075

bjnsn opened this issue Feb 20, 2018 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@bjnsn
Copy link

bjnsn commented Feb 20, 2018

TypeScript Version: 2.7.1, 2.7.2, 2.8.0-dev.20180220; issue not present in 2.6.2

Search Terms: overload, signature, generic, tuple

Code:

function foo<T1, T2>(values: [T1, T2]): [T1, T2];
function foo<T1>(values: [T1]): [T1];
function foo(values) {
	return values;
}

function fooWrapper<T1, T2>(values: [T1, T2]): [T1, T2];
/** [ts] Overload signature is not compatible with function implementation. **/
function fooWrapper<T1>(values: [T1]): [T1];
function fooWrapper(values) {
	return foo(values);
}

Expected behavior:
There should not be any type error; the wrapping function should behave identically to the wrapped function.

Actual behavior:
Compile time error on the overload signature of the wrapping function.

Playground Link: http://www.typescriptlang.org/play/#src=function%20foo%3CT1%2C%20T2%3E(values%3A%20%5BT1%2C%20T2%5D)%3A%20%5BT1%2C%20T2%5D%3B%0D%0Afunction%20foo%3CT1%3E(values%3A%20%5BT1%5D)%3A%20%5BT1%5D%3B%0D%0Afunction%20foo(values)%20%7B%0D%0A%09return%20values%3B%0D%0A%7D%0D%0A%0D%0Afunction%20fooWrapper%3CT1%2C%20T2%3E(values%3A%20%5BT1%2C%20T2%5D)%3A%20%5BT1%2C%20T2%5D%3B%0D%0A%2F**%20%5Bts%5D%20Overload%20signature%20is%20not%20compatible%20with%20function%20implementation.%20**%2F%0D%0Afunction%20fooWrapper%3CT1%3E(values%3A%20%5BT1%5D)%3A%20%5BT1%5D%3B%0D%0Afunction%20fooWrapper(values)%20%7B%0D%0A%09return%20foo(values)%3B%0D%0A%7D

Related Issues: None known

Notes: Seems potentially related to tuples fixed length change in 2.7

@bjnsn bjnsn changed the title Overload signature that uses generics breaks as 2.7.1 Overload signature that uses generics breaks as of 2.7.1 Feb 20, 2018
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 21, 2018

Seems potentially related to tuples fixed length change in 2.7

I suspect you're right about that. You can get around this by explicitly saying that fooWrapper returns an any[].

@bjnsn
Copy link
Author

bjnsn commented Feb 21, 2018

@DanielRosenwasser - yes:

function fooWrapper(values) {
	return foo(values) as any[];
}
const result = fooWrapper([1,2]); // [number, number]

or even

function fooWrapper(values) {
	return foo(values) as any;
}
const result = fooWrapper([1,2]); // [number, number]

solves the issue while retaining typings. I added this because it broke our CI and figured it might do the same for someone else later.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 21, 2018

ideally, you would give values a type annotation to be [any] | [any, any], and expect the type of foo(values) to be [any]|[any, any]. The issue here is that overload resolution does not distribute over union properties, so that is not possible today. this is tracked by #14107

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Feb 21, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants