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 — Improve constraints for non-homomorphic mapped types by ahejlsberg · Pull Request #29121 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Dec 21, 2018

Copy link
Copy Markdown
Member

This PR improves our logic for reasoning over constraints for non-homomorphic mapped types (such as Pick<T, K> and Record<K, T>). Previously we would only produce non-empty constraints for non-homomorphic mapped types with generic key types in very limited scenarios. Now we do much better. For example, all of the following were previously errors, but now none of them are:

function f0<T extends { a: string, b: string }>(obj: Pick<T, Extract<keyof T, 'b'>>) {
    obj.b;
}

function f1<T extends { a: string, b: string }>(obj: Pick<T, Exclude<keyof T, 'a'>>) {
    obj.b;
}

function f2<T extends { a: string, b: string }, U extends { b: string, c: string }>(obj: Pick<T | U, keyof (T | U)>) {
    obj.b;
}

function f3<T extends { a: string, b: string }, U extends { b: string, c: string }>(obj: Pick<T & U, keyof (T & U)>) {
    obj.a;
    obj.b;
    obj.c;
}

function f4<T extends { a: string, b: string }>(obj: Record<Exclude<keyof T, 'b'> | 'c', string>) {
    obj.a;
    obj.c;
}

Fixes #28821.

Sorry, something went wrong.

Uh oh!

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

MOZGIII and lucasbasquerotto reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
Anders Hejlsberg (ahejlsberg) added 3 commits December 21, 2018 12:50
@ahejlsberg
Properly compute lower bound of key type in a mapped type
565ab7a
@ahejlsberg
Add tests
7ad2661
@ahejlsberg
Accept new baselines
194496f
@ahejlsberg
Anders Hejlsberg (ahejlsberg) requested review from Daniel Rosenwasser (DanielRosenwasser) and Wesley Wigham (weswigham) December 21, 2018 21:04
@ahejlsberg
Anders Hejlsberg (ahejlsberg) merged commit 8570a67 into master Dec 31, 2018
@ahejlsberg
Anders Hejlsberg (ahejlsberg) deleted the mappedTypeConstraints branch December 31, 2018 03:44
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Jan 8, 2019
Generic spread expressions in object literals #28234
Merged
This was referenced Feb 1, 2019
Incorrect error when assigning to omitted and extended record type #27883
Closed
Generic mapped type constrained by a type parameter has different members / index signatures when constraint of that type parameter is a union #27819
Closed
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
@typescript-bot TypeScript Bot (typescript-bot) assigned Anders Hejlsberg (ahejlsberg) Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Awaiting requested review from DanielRosenwasser
@weswigham Wesley Wigham (weswigham) Awaiting requested review from weswigham

Uh oh!

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

Assignees

@ahejlsberg Anders Hejlsberg (ahejlsberg)

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.

1 participant

@ahejlsberg
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.