FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Contextual generic function types by ahejlsberg · Pull Request #16305 · microsoft/TypeScript · GitHub

Contextual generic function types - #16305

Merged
Anders Hejlsberg (ahejlsberg) merged 9 commits into
masterfrom
contextualGenericTypes
Jun 7, 2017
Merged

Contextual generic function types#16305
Anders Hejlsberg (ahejlsberg) merged 9 commits into
masterfrom
contextualGenericTypes

Conversation

Anders Hejlsberg (ahejlsberg) commented Jun 6, 2017
edited
Loading

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown

will miss this shirt #13039 (comment)

Comment thread src/compiler/checker.ts
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

type 'any'

Comment thread src/compiler/checker.ts
// 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

What is meant by "generic pure function type"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

A type that has nothing but a single call signature.

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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL