GeezerVison is showing github.com — Contextual generic function types by ahejlsberg · Pull Request #16305 · microsoft/TypeScript
Conversation
With this PR we do not erase the type parameters of contextual generic function types. Instead, the type parameters are allowed to propagate into the contextually typed expression. For example:
const f: <A>(a: A) => A = a => a; // Type of a is A
Previously, contextual generic function types were ignored and a would therefore have been of type any.
This PR also expands upon #16072 to permit the contextual type of a generic function to itself be a generic function type. For example:
const arrayMap = <T, U>(f: (x: T) => U) => (a: T[]) => a.map(f);
const arrayFilter = <T>(f: (x: T) => boolean) => (a: T[]) => a.filter(f);
const f1: (a: string[]) => number[] = arrayMap(x => x.length); // x: string
const f2: <A>(a: A[]) => A[][] = arrayMap(x => [x]); // x: A
const f3: <A>(a: A[]) => { value: A }[] = arrayMap(value => ({ value })); // value: A
const f4: (a: string[]) => string[] = arrayFilter(x => x.length > 10); // x: string
const f5: <T extends { value: number }>(a: T[]) => T[] = arrayFilter(x => x.value > 10); // x: T
Fixes #16293.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
zpdDG4gta8XKpMCd, aluanhaddad, Lenne231, filipesabella, masaeedu, jovdb, and SlurpTheo reacted with thumbs up emoji
zpdDG4gta8XKpMCd, aluanhaddad, msiemens, mquandalle, and olaf89 reacted with hooray emoji
zpdDG4gta8XKpMCd, DanielRosenwasser, aluanhaddad, alexrock, mquandalle, and alexeden reacted with heart emoji
All reactions
-
👍
7 reactions
-
🎉
5 reactions
-
❤️
6 reactions
RyanCavanaugh, ahejlsberg, aluanhaddad, alitaheri, SlurpTheo, tinganho, and peter-leonov reacted with laugh emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
const instantiatedType = instantiateType(contextualType, cloneTypeMapper(getContextualMapper(node)));
|
|
|
// If the contextual type is a generic pure function type, we instantiate the type with
|
|
|
// its own type parameters and type arguments. This ensures that the type parameters are
|
|
|
// not erased to type any during type inference such that they can be inferred as actual
|
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
// Inferences made from return types have lower priority than all other inferences.
|
|
|
inferTypes(context.inferences, instantiatedType, returnType, InferencePriority.ReturnType);
|
|
|
const instantiatedType = instantiateType(contextualType, cloneTypeMapper(getContextualMapper(node)));
|
|
|
// If the contextual type is a generic pure function type, we instantiate the type with
|
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.
What is meant by "generic pure function type"?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
A type that has nothing but a single call signature.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
+1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|