-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix Diff and Omit #21156
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
Merged
Merged
Fix Diff and Omit #21156
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12b80f3
Keep string index from intersections in base constraint of index type
sandersn baf31ec
Test Diff and Omit
sandersn d74820d
Remove mapped types to never from intersections
sandersn f1622f0
Use filter instead of unnecessary laziness
sandersn fd1bb9b
Group intersection code in getSimplifiedIndexedAccessType
sandersn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
15 changes: 15 additions & 0 deletions
15
tests/baselines/reference/indexedAccessRetainsIndexSignature.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//// [indexedAccessRetainsIndexSignature.ts] | ||
type Diff<T extends string, U extends string> = | ||
({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T] | ||
type Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>> | ||
type Omit1<U, K extends keyof U> = Pick<U, Diff<keyof U, K>>; | ||
// is in fact an equivalent of | ||
|
||
type Omit2<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]}; | ||
|
||
type O = Omit<{ a: number, b: string }, 'a'> | ||
const o: O = { b: '' } | ||
|
||
|
||
//// [indexedAccessRetainsIndexSignature.js] | ||
var o = { b: '' }; |
62 changes: 62 additions & 0 deletions
62
tests/baselines/reference/indexedAccessRetainsIndexSignature.symbols
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
=== tests/cases/compiler/indexedAccessRetainsIndexSignature.ts === | ||
type Diff<T extends string, U extends string> = | ||
>Diff : Symbol(Diff, Decl(indexedAccessRetainsIndexSignature.ts, 0, 0)) | ||
>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 0, 10)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 0, 27)) | ||
|
||
({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T] | ||
>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 1, 8)) | ||
>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 0, 10)) | ||
>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 1, 8)) | ||
>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 1, 26)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 0, 27)) | ||
>x : Symbol(x, Decl(indexedAccessRetainsIndexSignature.ts, 1, 48)) | ||
>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 0, 10)) | ||
|
||
type Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>> | ||
>Omit : Symbol(Omit, Decl(indexedAccessRetainsIndexSignature.ts, 1, 71)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 2, 10)) | ||
>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 2, 12)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 2, 10)) | ||
>Pick : Symbol(Pick, Decl(lib.d.ts, --, --)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 2, 10)) | ||
>Diff : Symbol(Diff, Decl(indexedAccessRetainsIndexSignature.ts, 0, 0)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 2, 10)) | ||
>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 2, 12)) | ||
|
||
type Omit1<U, K extends keyof U> = Pick<U, Diff<keyof U, K>>; | ||
>Omit1 : Symbol(Omit1, Decl(indexedAccessRetainsIndexSignature.ts, 2, 59)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 3, 11)) | ||
>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 3, 13)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 3, 11)) | ||
>Pick : Symbol(Pick, Decl(lib.d.ts, --, --)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 3, 11)) | ||
>Diff : Symbol(Diff, Decl(indexedAccessRetainsIndexSignature.ts, 0, 0)) | ||
>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 3, 11)) | ||
>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 3, 13)) | ||
|
||
// is in fact an equivalent of | ||
|
||
type Omit2<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]}; | ||
>Omit2 : Symbol(Omit2, Decl(indexedAccessRetainsIndexSignature.ts, 3, 61)) | ||
>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 6, 11)) | ||
>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 6, 13)) | ||
>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 6, 11)) | ||
>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 6, 37)) | ||
>Diff : Symbol(Diff, Decl(indexedAccessRetainsIndexSignature.ts, 0, 0)) | ||
>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 6, 11)) | ||
>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 6, 13)) | ||
>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 6, 11)) | ||
>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 6, 37)) | ||
|
||
type O = Omit<{ a: number, b: string }, 'a'> | ||
>O : Symbol(O, Decl(indexedAccessRetainsIndexSignature.ts, 6, 67)) | ||
>Omit : Symbol(Omit, Decl(indexedAccessRetainsIndexSignature.ts, 1, 71)) | ||
>a : Symbol(a, Decl(indexedAccessRetainsIndexSignature.ts, 8, 15)) | ||
>b : Symbol(b, Decl(indexedAccessRetainsIndexSignature.ts, 8, 26)) | ||
|
||
const o: O = { b: '' } | ||
>o : Symbol(o, Decl(indexedAccessRetainsIndexSignature.ts, 9, 5)) | ||
>O : Symbol(O, Decl(indexedAccessRetainsIndexSignature.ts, 6, 67)) | ||
>b : Symbol(b, Decl(indexedAccessRetainsIndexSignature.ts, 9, 14)) | ||
|
64 changes: 64 additions & 0 deletions
64
tests/baselines/reference/indexedAccessRetainsIndexSignature.types
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
=== tests/cases/compiler/indexedAccessRetainsIndexSignature.ts === | ||
type Diff<T extends string, U extends string> = | ||
>Diff : ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[T] | ||
>T : T | ||
>U : U | ||
|
||
({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T] | ||
>P : P | ||
>T : T | ||
>P : P | ||
>P : P | ||
>U : U | ||
>x : string | ||
>T : T | ||
|
||
type Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>> | ||
>Omit : Pick<U, ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[keyof U]> | ||
>U : U | ||
>K : K | ||
>U : U | ||
>Pick : Pick<T, K> | ||
>U : U | ||
>Diff : ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[T] | ||
>U : U | ||
>K : K | ||
|
||
type Omit1<U, K extends keyof U> = Pick<U, Diff<keyof U, K>>; | ||
>Omit1 : Pick<U, ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[keyof U]> | ||
>U : U | ||
>K : K | ||
>U : U | ||
>Pick : Pick<T, K> | ||
>U : U | ||
>Diff : ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[T] | ||
>U : U | ||
>K : K | ||
|
||
// is in fact an equivalent of | ||
|
||
type Omit2<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]}; | ||
>Omit2 : Omit2<T, K> | ||
>T : T | ||
>K : K | ||
>T : T | ||
>P : P | ||
>Diff : ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[T] | ||
>T : T | ||
>K : K | ||
>T : T | ||
>P : P | ||
|
||
type O = Omit<{ a: number, b: string }, 'a'> | ||
>O : Pick<{ a: number; b: string; }, "b"> | ||
>Omit : Pick<U, ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[keyof U]> | ||
>a : number | ||
>b : string | ||
|
||
const o: O = { b: '' } | ||
>o : Pick<{ a: number; b: string; }, "b"> | ||
>O : Pick<{ a: number; b: string; }, "b"> | ||
>{ b: '' } : { b: string; } | ||
>b : string | ||
>'' : "" | ||
|
10 changes: 10 additions & 0 deletions
10
tests/cases/compiler/indexedAccessRetainsIndexSignature.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
type Diff<T extends string, U extends string> = | ||
({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T] | ||
type Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>> | ||
type Omit1<U, K extends keyof U> = Pick<U, Diff<keyof U, K>>; | ||
// is in fact an equivalent of | ||
|
||
type Omit2<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]}; | ||
|
||
type O = Omit<{ a: number, b: string }, 'a'> | ||
const o: O = { b: '' } |
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.
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.
(on many people's wishlist): add these to
lib.d.ts
💟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.
Diff and Omit might someday be in lib.d.ts once conditional types are in Typescript, but the current implementation is way too hacky.
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.
In fact, only Diff implementation is way too hacky. But it will be solved if subtraction types make it into typescript (someday).