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. |
| // 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.
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.
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
sis 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.