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

Only consider function parameter types for generic type inference as a last resort #810

Closed
RyanCavanaugh opened this issue Oct 3, 2014 · 3 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@RyanCavanaugh
Copy link
Member

This is a rough sketch so I have something to refer to.

Currently, we treat the two instances of T in the parameter list here equally:

declare function containsAny<T>(arr: T[], predicate: (x: T) => boolean): boolean;

This means that when we make a call like this:

var x = containsAny([1, 2, 3], 'hello');

we infer T to be {}. The first argument is assignable to {}[] and the second argument is assignable to {}, so the call succeeds. This is unexpected and basically wrong.

If instead we only used the type parameters from function arguments when there were no other candidates for a given type parameter, we would infer T to be number and the call would be correctly rejected.

Not done yet: Actual spec work, thinking it through all the way, testing against existing code.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs More Info The issue still hasn't been fully clarified labels Oct 3, 2014
@RyanCavanaugh
Copy link
Member Author

This also matters in non-error cases:

declare function filter<T>(x: T[], y: (s: T) => boolean): T[];
var x = filter([1, 2, 3], Object.isExtensible); // x: any[]

We could infer x to be number[] instead with this change

@mhegazy
Copy link
Contributor

mhegazy commented May 18, 2017

looks to be the same issue as #1729 or at least related.

@RyanCavanaugh RyanCavanaugh added Fixed A PR has been merged for this issue and removed Needs More Info The issue still hasn't been fully clarified labels Apr 10, 2018
@RyanCavanaugh
Copy link
Member Author

We did this!

@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
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants