GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Contextual generic function types by ahejlsberg · Pull Request #16305 · microsoft/TypeScript

Conversation

@ahejlsberg

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

Uh oh!

There was an error while loading. Please reload this page.

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.

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
Anders Hejlsberg (ahejlsberg) added 7 commits June 3, 2017 16:44
@ahejlsberg
Include SymbolFlags.Function in couldContainTypeVariables check
dbf0362
@ahejlsberg
Preserve type parameters in generic contextual pure function types
9e613b9
@ahejlsberg
Allow contextual signature to be generic
0f6f857
@ahejlsberg
Fix fourslash tests
634c75c
@ahejlsberg
Accept new baselines
8a76939
@ahejlsberg
Add tests
61223f2
@ahejlsberg
Fix fourslash tests
34710a3
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-not-required label Jun 6, 2017
@ahejlsberg
Anders Hejlsberg (ahejlsberg) requested a review from Mohamed Hegazy (mhegazy) June 6, 2017 20:59
Anders Hejlsberg (ahejlsberg) added 2 commits June 6, 2017 14:57
@ahejlsberg
Merge branch 'master' into contextualGenericTypes
98a5c9b
@ahejlsberg
Accept new baselines
1c967c3
mhegazy
Mohamed Hegazy (mhegazy) approved these changes Jun 6, 2017
View reviewed changes
@zpdDG4gta8XKpMCd

ZpdDG4gta (zpdDG4gta8XKpMCd) commented Jun 6, 2017

Copy link
Copy Markdown

will miss this shirt #13039 (comment)

RyanCavanaugh, ahejlsberg, aluanhaddad, alitaheri, SlurpTheo, tinganho, and peter-leonov reacted with laugh emoji
All reactions
  • 😄 7 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser) reviewed Jun 7, 2017
View reviewed changes
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

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Jun 6, 2017

Copy link
Copy Markdown
Member

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.

type 'any'

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
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

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Jun 6, 2017

Copy link
Copy Markdown
Member

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.

All reactions

@ahejlsberg Anders Hejlsberg (ahejlsberg) Jun 7, 2017

Copy link
Copy Markdown
Member Author

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.

All reactions
@ahejlsberg
Anders Hejlsberg (ahejlsberg) merged commit 5888804 into master Jun 7, 2017
@ahejlsberg
Anders Hejlsberg (ahejlsberg) deleted the contextualGenericTypes branch June 7, 2017 00:49
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Jun 8, 2017
Stricter generic signature checks #16368
Merged
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Jun 9, 2017
Design Meeting Notes, 6/9/2017 #16415
Closed
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Jun 14, 2017
Circular inference failure (regression) #16512
Closed
@SlurpTheo SlurpTheo mentioned this pull request Jun 15, 2017
'Discarding Parameters' plus 'Duck Typing' makes every parameter optional? #9352
Closed
@DylanRJohnston Dylan R. Johnston (DylanRJohnston) mentioned this pull request Jun 15, 2017
Left and Right don't unify gcanti/fp-ts#128
Closed
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) DanielRosenwasser left review comments
+1 more reviewer
@mhegazy Mohamed Hegazy (mhegazy) mhegazy approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

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.

5 participants

@ahejlsberg @zpdDG4gta8XKpMCd @DanielRosenwasser @mhegazy @msftclas
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.