Skip to content

Commit 597a9c6

Browse files
committed
Include all properties from the mapped modifier type when calculating index types for mapped types with name types
1 parent 422fd19 commit 597a9c6

5 files changed

+177
-1
lines changed

src/compiler/checker.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -13614,11 +13614,18 @@ namespace ts {
1361413614
type.resolvedIndexType || (type.resolvedIndexType = createIndexType(type, /*stringsOnly*/ false));
1361513615
}
1361613616

13617+
function instantiateTypeAsMappedNameType(nameType: Type, type: MappedType, t: Type) {
13618+
return instantiateType(nameType, appendTypeMapping(type.mapper, getTypeParameterFromMappedType(type), t));
13619+
}
13620+
1361713621
function getIndexTypeForMappedType(type: MappedType, noIndexSignatures: boolean | undefined) {
1361813622
const constraint = filterType(getConstraintTypeFromMappedType(type), t => !(noIndexSignatures && t.flags & (TypeFlags.Any | TypeFlags.String)));
1361913623
const nameType = type.declaration.nameType && getTypeFromTypeNode(type.declaration.nameType);
13624+
// If the constraint is exclusively string/number/never type(s), we need to pull the property names from the modified type and run them through the `nameType` mapper as well
13625+
// since they won't appear in the constraint, due to subtype reducing with the string/number index types
13626+
const properties = nameType && everyType(constraint, t => !!(t.flags & (TypeFlags.String | TypeFlags.Number | TypeFlags.Never))) && getPropertiesOfType(getApparentType(getModifiersTypeFromMappedType(type)));
1362013627
return nameType ?
13621-
mapType(constraint, t => instantiateType(nameType, appendTypeMapping(type.mapper, getTypeParameterFromMappedType(type), t))) :
13628+
getUnionType([mapType(constraint, t => instantiateTypeAsMappedNameType(nameType, type, t)), mapType(getUnionType(map(properties || emptyArray, p => getLiteralTypeFromProperty(p, TypeFlags.StringOrNumberLiteralOrUnique))), t => instantiateTypeAsMappedNameType(nameType, type, t))]):
1362213629
constraint;
1362313630
}
1362413631

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [computedTypesKeyofNoIndexSignatureType.ts]
2+
type Compute<A> = { [K in keyof A]: Compute<A[K]>; } & {};
3+
4+
type EqualsTest<T> = <A>() => A extends T ? 1 : 0;
5+
type Equals<A1, A2> = EqualsTest<A2> extends EqualsTest<A1> ? 1 : 0;
6+
7+
type Filter<K, I> = Equals<K, I> extends 1 ? never : K;
8+
9+
type OmitIndex<T, I extends string | number> = {
10+
[K in keyof T as Filter<K, I>]: T[K];
11+
};
12+
13+
type IndexObject = { [key: string]: unknown; };
14+
type FooBar = { foo: "hello"; bar: "world"; };
15+
16+
type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK
17+
type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK
18+
19+
type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK
20+
type WithIndexKey = keyof WithIndex; // string | number <-- Expected: string
21+
type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar"
22+
23+
//// [computedTypesKeyofNoIndexSignatureType.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
=== tests/cases/compiler/computedTypesKeyofNoIndexSignatureType.ts ===
2+
type Compute<A> = { [K in keyof A]: Compute<A[K]>; } & {};
3+
>Compute : Symbol(Compute, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 0))
4+
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 13))
5+
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 21))
6+
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 13))
7+
>Compute : Symbol(Compute, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 0))
8+
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 13))
9+
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 21))
10+
11+
type EqualsTest<T> = <A>() => A extends T ? 1 : 0;
12+
>EqualsTest : Symbol(EqualsTest, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 58))
13+
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 16))
14+
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 22))
15+
>A : Symbol(A, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 22))
16+
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 16))
17+
18+
type Equals<A1, A2> = EqualsTest<A2> extends EqualsTest<A1> ? 1 : 0;
19+
>Equals : Symbol(Equals, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 50))
20+
>A1 : Symbol(A1, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 12))
21+
>A2 : Symbol(A2, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 15))
22+
>EqualsTest : Symbol(EqualsTest, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 58))
23+
>A2 : Symbol(A2, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 15))
24+
>EqualsTest : Symbol(EqualsTest, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 58))
25+
>A1 : Symbol(A1, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 12))
26+
27+
type Filter<K, I> = Equals<K, I> extends 1 ? never : K;
28+
>Filter : Symbol(Filter, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 68))
29+
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 12))
30+
>I : Symbol(I, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 14))
31+
>Equals : Symbol(Equals, Decl(computedTypesKeyofNoIndexSignatureType.ts, 2, 50))
32+
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 12))
33+
>I : Symbol(I, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 14))
34+
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 12))
35+
36+
type OmitIndex<T, I extends string | number> = {
37+
>OmitIndex : Symbol(OmitIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 55))
38+
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 15))
39+
>I : Symbol(I, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 17))
40+
41+
[K in keyof T as Filter<K, I>]: T[K];
42+
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 8, 3))
43+
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 15))
44+
>Filter : Symbol(Filter, Decl(computedTypesKeyofNoIndexSignatureType.ts, 3, 68))
45+
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 8, 3))
46+
>I : Symbol(I, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 17))
47+
>T : Symbol(T, Decl(computedTypesKeyofNoIndexSignatureType.ts, 7, 15))
48+
>K : Symbol(K, Decl(computedTypesKeyofNoIndexSignatureType.ts, 8, 3))
49+
50+
};
51+
52+
type IndexObject = { [key: string]: unknown; };
53+
>IndexObject : Symbol(IndexObject, Decl(computedTypesKeyofNoIndexSignatureType.ts, 9, 2))
54+
>key : Symbol(key, Decl(computedTypesKeyofNoIndexSignatureType.ts, 11, 22))
55+
56+
type FooBar = { foo: "hello"; bar: "world"; };
57+
>FooBar : Symbol(FooBar, Decl(computedTypesKeyofNoIndexSignatureType.ts, 11, 47))
58+
>foo : Symbol(foo, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 15))
59+
>bar : Symbol(bar, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 29))
60+
61+
type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK
62+
>WithIndex : Symbol(WithIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 46))
63+
>Compute : Symbol(Compute, Decl(computedTypesKeyofNoIndexSignatureType.ts, 0, 0))
64+
>FooBar : Symbol(FooBar, Decl(computedTypesKeyofNoIndexSignatureType.ts, 11, 47))
65+
>IndexObject : Symbol(IndexObject, Decl(computedTypesKeyofNoIndexSignatureType.ts, 9, 2))
66+
67+
type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK
68+
>WithoutIndex : Symbol(WithoutIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 14, 47))
69+
>OmitIndex : Symbol(OmitIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 5, 55))
70+
>WithIndex : Symbol(WithIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 46))
71+
72+
type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK
73+
>FooBarKey : Symbol(FooBarKey, Decl(computedTypesKeyofNoIndexSignatureType.ts, 15, 49))
74+
>FooBar : Symbol(FooBar, Decl(computedTypesKeyofNoIndexSignatureType.ts, 11, 47))
75+
76+
type WithIndexKey = keyof WithIndex; // string | number <-- Expected: string
77+
>WithIndexKey : Symbol(WithIndexKey, Decl(computedTypesKeyofNoIndexSignatureType.ts, 17, 30))
78+
>WithIndex : Symbol(WithIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 12, 46))
79+
80+
type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar"
81+
>WithoutIndexKey : Symbol(WithoutIndexKey, Decl(computedTypesKeyofNoIndexSignatureType.ts, 18, 36))
82+
>WithoutIndex : Symbol(WithoutIndex, Decl(computedTypesKeyofNoIndexSignatureType.ts, 14, 47))
83+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
=== tests/cases/compiler/computedTypesKeyofNoIndexSignatureType.ts ===
2+
type Compute<A> = { [K in keyof A]: Compute<A[K]>; } & {};
3+
>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; }; }; }; }; }; }; }; }; }; }; }
4+
5+
type EqualsTest<T> = <A>() => A extends T ? 1 : 0;
6+
>EqualsTest : EqualsTest<T>
7+
8+
type Equals<A1, A2> = EqualsTest<A2> extends EqualsTest<A1> ? 1 : 0;
9+
>Equals : Equals<A1, A2>
10+
11+
type Filter<K, I> = Equals<K, I> extends 1 ? never : K;
12+
>Filter : Filter<K, I>
13+
14+
type OmitIndex<T, I extends string | number> = {
15+
>OmitIndex : OmitIndex<T, I>
16+
17+
[K in keyof T as Filter<K, I>]: T[K];
18+
};
19+
20+
type IndexObject = { [key: string]: unknown; };
21+
>IndexObject : IndexObject
22+
>key : string
23+
24+
type FooBar = { foo: "hello"; bar: "world"; };
25+
>FooBar : FooBar
26+
>foo : "hello"
27+
>bar : "world"
28+
29+
type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK
30+
>WithIndex : { [x: string]: {}; foo: "hello"; bar: "world"; }
31+
32+
type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK
33+
>WithoutIndex : OmitIndex<{ [x: string]: {}; foo: "hello"; bar: "world"; }, string>
34+
35+
type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK
36+
>FooBarKey : "foo" | "bar"
37+
38+
type WithIndexKey = keyof WithIndex; // string | number <-- Expected: string
39+
>WithIndexKey : string | number
40+
41+
type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar"
42+
>WithoutIndexKey : number | "foo" | "bar"
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type Compute<A> = { [K in keyof A]: Compute<A[K]>; } & {};
2+
3+
type EqualsTest<T> = <A>() => A extends T ? 1 : 0;
4+
type Equals<A1, A2> = EqualsTest<A2> extends EqualsTest<A1> ? 1 : 0;
5+
6+
type Filter<K, I> = Equals<K, I> extends 1 ? never : K;
7+
8+
type OmitIndex<T, I extends string | number> = {
9+
[K in keyof T as Filter<K, I>]: T[K];
10+
};
11+
12+
type IndexObject = { [key: string]: unknown; };
13+
type FooBar = { foo: "hello"; bar: "world"; };
14+
15+
type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK
16+
type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK
17+
18+
type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK
19+
type WithIndexKey = keyof WithIndex; // string | number <-- Expected: string
20+
type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar"

0 commit comments

Comments
 (0)