Skip to content

Commit 88c6825

Browse files
authored
Merge pull request #13234 from Microsoft/fixMappedTypeInstantiation
Fix mapped type instantiation
2 parents 524fa64 + c0bf7de commit 88c6825

File tree

5 files changed

+111
-13
lines changed

5 files changed

+111
-13
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6652,17 +6652,19 @@ namespace ts {
66526652
const constraintType = getConstraintTypeFromMappedType(type);
66536653
if (constraintType.flags & TypeFlags.Index) {
66546654
const typeVariable = (<IndexType>constraintType).type;
6655-
const mappedTypeVariable = instantiateType(typeVariable, mapper);
6656-
if (typeVariable !== mappedTypeVariable) {
6657-
return mapType(mappedTypeVariable, t => {
6658-
if (isMappableType(t)) {
6659-
const replacementMapper = createUnaryTypeMapper(typeVariable, t);
6660-
const combinedMapper = mapper.mappedTypes && mapper.mappedTypes.length === 1 ? replacementMapper : combineTypeMappers(replacementMapper, mapper);
6661-
combinedMapper.mappedTypes = mapper.mappedTypes;
6662-
return instantiateMappedObjectType(type, combinedMapper);
6663-
}
6664-
return t;
6665-
});
6655+
if (typeVariable.flags & TypeFlags.TypeParameter) {
6656+
const mappedTypeVariable = instantiateType(typeVariable, mapper);
6657+
if (typeVariable !== mappedTypeVariable) {
6658+
return mapType(mappedTypeVariable, t => {
6659+
if (isMappableType(t)) {
6660+
const replacementMapper = createUnaryTypeMapper(typeVariable, t);
6661+
const combinedMapper = mapper.mappedTypes && mapper.mappedTypes.length === 1 ? replacementMapper : combineTypeMappers(replacementMapper, mapper);
6662+
combinedMapper.mappedTypes = mapper.mappedTypes;
6663+
return instantiateMappedObjectType(type, combinedMapper);
6664+
}
6665+
return t;
6666+
});
6667+
}
66666668
}
66676669
}
66686670
return instantiateMappedObjectType(type, mapper);

tests/baselines/reference/mappedTypes4.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,19 @@ type DeepReadonlyFoo = {
5858
};
5959

6060
var x1: DeepReadonly<Foo>;
61-
var x1: DeepReadonlyFoo;
61+
var x1: DeepReadonlyFoo;
62+
63+
// Repro from #13232
64+
65+
type Z = { a: number };
66+
type Clone<T> = {
67+
[P in keyof (T & {})]: T[P];
68+
};
69+
type M = Clone<Z>; // M should be { a: number }
70+
71+
var z1: Z;
72+
var z1: Clone<Z>;
73+
6274

6375
//// [mappedTypes4.js]
6476
function boxify(obj) {
@@ -76,6 +88,8 @@ function f1(x) {
7688
}
7789
var x1;
7890
var x1;
91+
var z1;
92+
var z1;
7993

8094

8195
//// [mappedTypes4.d.ts]
@@ -127,3 +141,12 @@ declare type DeepReadonlyFoo = {
127141
};
128142
declare var x1: DeepReadonly<Foo>;
129143
declare var x1: DeepReadonlyFoo;
144+
declare type Z = {
145+
a: number;
146+
};
147+
declare type Clone<T> = {
148+
[P in keyof (T & {})]: T[P];
149+
};
150+
declare type M = Clone<Z>;
151+
declare var z1: Z;
152+
declare var z1: Clone<Z>;

tests/baselines/reference/mappedTypes4.symbols

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,34 @@ var x1: DeepReadonlyFoo;
196196
>x1 : Symbol(x1, Decl(mappedTypes4.ts, 58, 3), Decl(mappedTypes4.ts, 59, 3))
197197
>DeepReadonlyFoo : Symbol(DeepReadonlyFoo, Decl(mappedTypes4.ts, 50, 2))
198198

199+
// Repro from #13232
200+
201+
type Z = { a: number };
202+
>Z : Symbol(Z, Decl(mappedTypes4.ts, 59, 24))
203+
>a : Symbol(a, Decl(mappedTypes4.ts, 63, 10))
204+
205+
type Clone<T> = {
206+
>Clone : Symbol(Clone, Decl(mappedTypes4.ts, 63, 23))
207+
>T : Symbol(T, Decl(mappedTypes4.ts, 64, 11))
208+
209+
[P in keyof (T & {})]: T[P];
210+
>P : Symbol(P, Decl(mappedTypes4.ts, 65, 3))
211+
>T : Symbol(T, Decl(mappedTypes4.ts, 64, 11))
212+
>T : Symbol(T, Decl(mappedTypes4.ts, 64, 11))
213+
>P : Symbol(P, Decl(mappedTypes4.ts, 65, 3))
214+
215+
};
216+
type M = Clone<Z>; // M should be { a: number }
217+
>M : Symbol(M, Decl(mappedTypes4.ts, 66, 2))
218+
>Clone : Symbol(Clone, Decl(mappedTypes4.ts, 63, 23))
219+
>Z : Symbol(Z, Decl(mappedTypes4.ts, 59, 24))
220+
221+
var z1: Z;
222+
>z1 : Symbol(z1, Decl(mappedTypes4.ts, 69, 3), Decl(mappedTypes4.ts, 70, 3))
223+
>Z : Symbol(Z, Decl(mappedTypes4.ts, 59, 24))
224+
225+
var z1: Clone<Z>;
226+
>z1 : Symbol(z1, Decl(mappedTypes4.ts, 69, 3), Decl(mappedTypes4.ts, 70, 3))
227+
>Clone : Symbol(Clone, Decl(mappedTypes4.ts, 63, 23))
228+
>Z : Symbol(Z, Decl(mappedTypes4.ts, 59, 24))
229+

tests/baselines/reference/mappedTypes4.types

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,34 @@ var x1: DeepReadonlyFoo;
211211
>x1 : DeepReadonly<Foo>
212212
>DeepReadonlyFoo : DeepReadonlyFoo
213213

214+
// Repro from #13232
215+
216+
type Z = { a: number };
217+
>Z : Z
218+
>a : number
219+
220+
type Clone<T> = {
221+
>Clone : Clone<T>
222+
>T : T
223+
224+
[P in keyof (T & {})]: T[P];
225+
>P : P
226+
>T : T
227+
>T : T
228+
>P : P
229+
230+
};
231+
type M = Clone<Z>; // M should be { a: number }
232+
>M : Clone<Z>
233+
>Clone : Clone<T>
234+
>Z : Z
235+
236+
var z1: Z;
237+
>z1 : Z
238+
>Z : Z
239+
240+
var z1: Clone<Z>;
241+
>z1 : Z
242+
>Clone : Clone<T>
243+
>Z : Z
244+

tests/cases/conformance/types/mapped/mappedTypes4.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,15 @@ type DeepReadonlyFoo = {
5959
};
6060

6161
var x1: DeepReadonly<Foo>;
62-
var x1: DeepReadonlyFoo;
62+
var x1: DeepReadonlyFoo;
63+
64+
// Repro from #13232
65+
66+
type Z = { a: number };
67+
type Clone<T> = {
68+
[P in keyof (T & {})]: T[P];
69+
};
70+
type M = Clone<Z>; // M should be { a: number }
71+
72+
var z1: Z;
73+
var z1: Clone<Z>;

0 commit comments

Comments
 (0)