-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Infer from generic function return types #16072
Conversation
I was told "if you get Anders interested in your typings issue, it'll be implemented in a few hours" Apparently the legend is real. |
src/compiler/checker.ts
Outdated
@@ -10288,27 +10293,24 @@ namespace ts { | |||
// it as an inference candidate. Hopefully, a better candidate will come along that does | |||
// not contain anyFunctionType when we come back to this argument for its second round | |||
// of inference. | |||
if (source.flags & TypeFlags.ContainsAnyFunctionType) { | |||
if (source.flags & TypeFlags.ContainsAnyFunctionType || source === silentNeverType) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worth adding a note on the use of silentNeverType as a marker here.
src/compiler/checker.ts
Outdated
inferences.primary || (inferences.primary = []); | ||
if (!contains(candidates, source)) { | ||
candidates.push(source); | ||
for (const inference of inferences) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getInferenceInfoForType
here as well?
Why doesn't this fix #11152 ? |
contextual type is not propagated/instantiated correctly yet. |
If that's still outstanding, might there be any PR/branch we could follow on that? |
@mhegazy - I'm liking the use of the word "yet" above 👍 😄 |
Where can we track progress against the following bug? type Function1<T1, R> = (t1: T1) => R
function pipe<T1, R>(fn: Function1<T1, R>): Function1<T1, R> {
return fn
}
/// test
function identity<A>(a: A): A { return a }
const pipedIdentity = pipe(identity)
// x is a {}, not a number
const x = pipedIdentity(33) |
@AlexGalays see #9366 and the linked issues. |
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.