Skip to content
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

Contextual generic function types #16305

Merged
merged 9 commits into from
Jun 7, 2017
Merged

Contextual generic function types #16305

merged 9 commits into from
Jun 7, 2017

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Jun 6, 2017

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.

@zpdDG4gta8XKpMCd
Copy link

will miss this shirt #13039 (comment)

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
Member

Choose a reason for hiding this comment

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

type 'any'

// 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
Member

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"?

Copy link
Member Author

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.

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