-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Include all properties from the mapped modifier type when calculating… #41976
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
weswigham
merged 2 commits into
microsoft:master
from
weswigham:include-modifier-index-props
Jan 27, 2021
Merged
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions
23
tests/baselines/reference/computedTypesKeyofNoIndexSignatureType.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,23 @@ | ||
//// [computedTypesKeyofNoIndexSignatureType.ts] | ||
type Compute<A> = { [K in keyof A]: Compute<A[K]>; } & {}; | ||
|
||
type EqualsTest<T> = <A>() => A extends T ? 1 : 0; | ||
type Equals<A1, A2> = EqualsTest<A2> extends EqualsTest<A1> ? 1 : 0; | ||
|
||
type Filter<K, I> = Equals<K, I> extends 1 ? never : K; | ||
|
||
type OmitIndex<T, I extends string | number> = { | ||
[K in keyof T as Filter<K, I>]: T[K]; | ||
}; | ||
|
||
type IndexObject = { [key: string]: unknown; }; | ||
type FooBar = { foo: "hello"; bar: "world"; }; | ||
|
||
type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK | ||
type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK | ||
|
||
type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK | ||
type WithIndexKey = keyof WithIndex; // string | number <-- Expected: string | ||
type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar" | ||
|
||
//// [computedTypesKeyofNoIndexSignatureType.js] |
83 changes: 83 additions & 0 deletions
83
tests/baselines/reference/computedTypesKeyofNoIndexSignatureType.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,83 @@ | ||
=== tests/cases/compiler/computedTypesKeyofNoIndexSignatureType.ts === | ||
type Compute<A> = { [K in keyof A]: Compute<A[K]>; } & {}; | ||
>Compute : Symbol(Compute, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 0)) | ||
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 13)) | ||
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 21)) | ||
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 13)) | ||
>Compute : Symbol(Compute, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 0)) | ||
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 13)) | ||
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 21)) | ||
|
||
type EqualsTest<T> = <A>() => A extends T ? 1 : 0; | ||
>EqualsTest : Symbol(EqualsTest, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 58)) | ||
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 16)) | ||
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 22)) | ||
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 22)) | ||
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 16)) | ||
|
||
type Equals<A1, A2> = EqualsTest<A2> extends EqualsTest<A1> ? 1 : 0; | ||
>Equals : Symbol(Equals, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 50)) | ||
>A1 : Symbol(A1, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 12)) | ||
>A2 : Symbol(A2, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 15)) | ||
>EqualsTest : Symbol(EqualsTest, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 58)) | ||
>A2 : Symbol(A2, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 15)) | ||
>EqualsTest : Symbol(EqualsTest, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 58)) | ||
>A1 : Symbol(A1, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 12)) | ||
|
||
type Filter<K, I> = Equals<K, I> extends 1 ? never : K; | ||
>Filter : Symbol(Filter, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 68)) | ||
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 12)) | ||
>I : Symbol(I, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 14)) | ||
>Equals : Symbol(Equals, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 50)) | ||
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 12)) | ||
>I : Symbol(I, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 14)) | ||
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 12)) | ||
|
||
type OmitIndex<T, I extends string | number> = { | ||
>OmitIndex : Symbol(OmitIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 55)) | ||
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 15)) | ||
>I : Symbol(I, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 17)) | ||
|
||
[K in keyof T as Filter<K, I>]: T[K]; | ||
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 8, 3)) | ||
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 15)) | ||
>Filter : Symbol(Filter, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 68)) | ||
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 8, 3)) | ||
>I : Symbol(I, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 17)) | ||
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 15)) | ||
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 8, 3)) | ||
|
||
}; | ||
|
||
type IndexObject = { [key: string]: unknown; }; | ||
>IndexObject : Symbol(IndexObject, Decl(computedTypesKeyofNoIndexSignatureType.ts, 9, 2)) | ||
>key : Symbol(key, Decl(computedTypesKeyofNoIndexSignatureType.ts, 11, 22)) | ||
|
||
type FooBar = { foo: "hello"; bar: "world"; }; | ||
>FooBar : Symbol(FooBar, Decl(computedTypesKeyofNoIndexSignatureType.ts, 11, 47)) | ||
>foo : Symbol(foo, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 15)) | ||
>bar : Symbol(bar, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 29)) | ||
|
||
type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK | ||
>WithIndex : Symbol(WithIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 46)) | ||
>Compute : Symbol(Compute, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 0)) | ||
>FooBar : Symbol(FooBar, Decl(computedTypesKeyofNoIndexSignatureType.ts, 11, 47)) | ||
>IndexObject : Symbol(IndexObject, Decl(computedTypesKeyofNoIndexSignatureType.ts, 9, 2)) | ||
|
||
type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK | ||
>WithoutIndex : Symbol(WithoutIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 14, 47)) | ||
>OmitIndex : Symbol(OmitIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 55)) | ||
>WithIndex : Symbol(WithIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 46)) | ||
|
||
type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK | ||
>FooBarKey : Symbol(FooBarKey, Decl(computedTypesKeyofNoIndexSignatureType.ts, 15, 49)) | ||
>FooBar : Symbol(FooBar, Decl(computedTypesKeyofNoIndexSignatureType.ts, 11, 47)) | ||
|
||
type WithIndexKey = keyof WithIndex; // string | number <-- Expected: string | ||
>WithIndexKey : Symbol(WithIndexKey, Decl(computedTypesKeyofNoIndexSignatureType.ts, 17, 30)) | ||
>WithIndex : Symbol(WithIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 46)) | ||
|
||
type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar" | ||
>WithoutIndexKey : Symbol(WithoutIndexKey, Decl(computedTypesKeyofNoIndexSignatureType.ts, 18, 36)) | ||
>WithoutIndex : Symbol(WithoutIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 14, 47)) | ||
|
43 changes: 43 additions & 0 deletions
43
tests/baselines/reference/computedTypesKeyofNoIndexSignatureType.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,43 @@ | ||
=== tests/cases/compiler/computedTypesKeyofNoIndexSignatureType.ts === | ||
type Compute<A> = { [K in keyof A]: Compute<A[K]>; } & {}; | ||
>Compute : { [K in keyof A]: { [K in keyof A[K]]: { [K in keyof A[K][K]]: { [K in keyof A[K][K][K]]: { [K in keyof A[K][K][K][K]]: { [K in keyof A[K][K][K][K][K]]: { [K in keyof A[K][K][K][K][K][K]]: { [K in keyof A[K][K][K][K][K][K][K]]: { [K in keyof A[K][K][K][K][K][K][K][K]]: { [K in keyof A[K][K][K][K][K][K][K][K][K]]: { [K in keyof A[K][K][K][K][K][K][K][K][K][K]]: any; }; }; }; }; }; }; }; }; }; }; } | ||
|
||
type EqualsTest<T> = <A>() => A extends T ? 1 : 0; | ||
>EqualsTest : EqualsTest<T> | ||
|
||
type Equals<A1, A2> = EqualsTest<A2> extends EqualsTest<A1> ? 1 : 0; | ||
>Equals : Equals<A1, A2> | ||
|
||
type Filter<K, I> = Equals<K, I> extends 1 ? never : K; | ||
>Filter : Filter<K, I> | ||
|
||
type OmitIndex<T, I extends string | number> = { | ||
>OmitIndex : OmitIndex<T, I> | ||
|
||
[K in keyof T as Filter<K, I>]: T[K]; | ||
}; | ||
|
||
type IndexObject = { [key: string]: unknown; }; | ||
>IndexObject : IndexObject | ||
>key : string | ||
|
||
type FooBar = { foo: "hello"; bar: "world"; }; | ||
>FooBar : FooBar | ||
>foo : "hello" | ||
>bar : "world" | ||
|
||
type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK | ||
>WithIndex : { [x: string]: {}; foo: "hello"; bar: "world"; } | ||
|
||
type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK | ||
>WithoutIndex : OmitIndex<{ [x: string]: {}; foo: "hello"; bar: "world"; }, string> | ||
|
||
type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK | ||
>FooBarKey : keyof FooBar | ||
|
||
type WithIndexKey = keyof WithIndex; // string | number <-- Expected: string | ||
>WithIndexKey : string | number | ||
|
||
type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar" | ||
>WithoutIndexKey : number | "foo" | "bar" | ||
|
20 changes: 20 additions & 0 deletions
20
tests/cases/compiler/computedTypesKeyofNoIndexSignatureType.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,20 @@ | ||
type Compute<A> = { [K in keyof A]: Compute<A[K]>; } & {}; | ||
|
||
type EqualsTest<T> = <A>() => A extends T ? 1 : 0; | ||
type Equals<A1, A2> = EqualsTest<A2> extends EqualsTest<A1> ? 1 : 0; | ||
|
||
type Filter<K, I> = Equals<K, I> extends 1 ? never : K; | ||
|
||
type OmitIndex<T, I extends string | number> = { | ||
[K in keyof T as Filter<K, I>]: T[K]; | ||
}; | ||
|
||
type IndexObject = { [key: string]: unknown; }; | ||
type FooBar = { foo: "hello"; bar: "world"; }; | ||
|
||
type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK | ||
type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK | ||
|
||
type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK | ||
type WithIndexKey = keyof WithIndex; // string | number <-- Expected: string | ||
type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar" |
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.
Is this right?
WithoutIndex
appears, at least in quick info, not to have an index signature at all (not sure if it’s represented differently under the hood), so it seems like you shouldn’t be allowed to index into it withnumber
?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.
Slight simplification of the test case, FWIW:
Uh oh!
There was an error while loading. Please reload this page.
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.
Arguable - the
OmitIndex
type is only omitting thestring
key, however, astring
index signature adds both astring
andnumber
key to thekeyof
of the type, since astring
index signature implies and allows indexing bynumber
!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.
That makes sense, but two follow up questions:
WithoutIndex
?{ [K in keyof { [key: string]: any }]: K }
be understood? Why isn’t it{ [x: string]: string | number }
or{ [x: string]: string & number }
or{ [x: string]: string, [x: number]: number }
?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.
For 1: The keyof logic includes
number
in the key type, however mapped type mappings iterate over the set of properties/indexes in the originalmodifiersType
directly if available (and homomorphic), leading to the discrepancy.As for 2: We have no principled answer and our behaviors are arbitrarily chosen. I think I went over the inconsistency at a design meeting just after key mappings were introduced and we decided to stick with all the inconsistencies so we didn't break anyone.
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.
It feels troubling that
is an error even though
keyof typeof withoutIndex
includesnumber
. Is there any way to observe the numberyness of the keys besideskeyof
?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.
is already how keyof already works, so, we allow
The
number
-yness of string indexers is pretty visible everywhere it can be shown, IMO. I can't think of somewhere we actually forbid using a number on a string index signature... such a location would probably constitute a bug.Uh oh!
There was an error while loading. Please reload this page.
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.
You can blame this on how we construct mapped types. While the
keyof
result excludes string, mapped type construction actually doesn't usekeyof
at all for homomorphic mapped types (heh), and instead iterates over the slots visible on the modifiers type. If thestring
indexer has been filtered out it doesn't check "oh, well, maybe anumber
indexer is still appropriate given the filter applied to the modifier", it just drops it.In any case,
number
is present in the keys is true both before and after this change, so I'm thinking probably doesn't have too much bearing on this change as-is? That could be a separate enhancement/change to how we create mapped types withas
clauses.