You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
With this PR we improve type argument inference by inferring from the contextual type of a generic function call to the return type of the generic function. For example:
functionemptyArray<T>(): T[]{return[];}leta1: string[]=emptyArray();// string inferred for Tleta2: number[]=emptyArray();// number inferred for T
Previously the two assignments above would error because {} was inferred for T. We now infer from the contextual type (i.e. the type of the variable to which the function result is assigned).
Inferences made from generic function return types have lower priority than inferences made from arguments to the generic function. For example, the type of s is inferred as string (not Object) in the following:
Where can we track progress against the following bug?
typeFunction1<T1,R>=(t1: T1)=>Rfunctionpipe<T1,R>(fn: Function1<T1,R>): Function1<T1,R>{returnfn}/// testfunctionidentity<A>(a: A): A{returna}constpipedIdentity=pipe(identity)// x is a {}, not a numberconstx=pipedIdentity(33)
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
None yet
9 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With this PR we improve type argument inference by inferring from the contextual type of a generic function call to the return type of the generic function. For example:
Previously the two assignments above would error because
{}
was inferred forT
. We now infer from the contextual type (i.e. the type of the variable to which the function result is assigned).A more elaborate example:
Inferences made from generic function return types have lower priority than inferences made from arguments to the generic function. For example, the type of
s
is inferred asstring
(notObject
) in the following:This PR is a precursor for inferring higher order function types when no inferences can be made for one or more type parameters.
We currently infer
(x: {}) => { p: {}[] }
, but ideally we'd infer<A>(x: A) => { p: A[] }
. That's next on the list.Fixes #15680.