Merge pull request #13234 from Microsoft/fixMappedTypeInstantiation

Fix mapped type instantiation
This commit is contained in:
Anders Hejlsberg 2016-12-31 07:48:48 -10:00 committed by GitHub
commit 88c68256e6
5 changed files with 111 additions and 13 deletions

View file

@ -6652,17 +6652,19 @@ namespace ts {
const constraintType = getConstraintTypeFromMappedType(type);
if (constraintType.flags & TypeFlags.Index) {
const typeVariable = (<IndexType>constraintType).type;
const mappedTypeVariable = instantiateType(typeVariable, mapper);
if (typeVariable !== mappedTypeVariable) {
return mapType(mappedTypeVariable, t => {
if (isMappableType(t)) {
const replacementMapper = createUnaryTypeMapper(typeVariable, t);
const combinedMapper = mapper.mappedTypes && mapper.mappedTypes.length === 1 ? replacementMapper : combineTypeMappers(replacementMapper, mapper);
combinedMapper.mappedTypes = mapper.mappedTypes;
return instantiateMappedObjectType(type, combinedMapper);
}
return t;
});
if (typeVariable.flags & TypeFlags.TypeParameter) {
const mappedTypeVariable = instantiateType(typeVariable, mapper);
if (typeVariable !== mappedTypeVariable) {
return mapType(mappedTypeVariable, t => {
if (isMappableType(t)) {
const replacementMapper = createUnaryTypeMapper(typeVariable, t);
const combinedMapper = mapper.mappedTypes && mapper.mappedTypes.length === 1 ? replacementMapper : combineTypeMappers(replacementMapper, mapper);
combinedMapper.mappedTypes = mapper.mappedTypes;
return instantiateMappedObjectType(type, combinedMapper);
}
return t;
});
}
}
}
return instantiateMappedObjectType(type, mapper);

View file

@ -58,7 +58,19 @@ type DeepReadonlyFoo = {
};
var x1: DeepReadonly<Foo>;
var x1: DeepReadonlyFoo;
var x1: DeepReadonlyFoo;
// Repro from #13232
type Z = { a: number };
type Clone<T> = {
[P in keyof (T & {})]: T[P];
};
type M = Clone<Z>; // M should be { a: number }
var z1: Z;
var z1: Clone<Z>;
//// [mappedTypes4.js]
function boxify(obj) {
@ -76,6 +88,8 @@ function f1(x) {
}
var x1;
var x1;
var z1;
var z1;
//// [mappedTypes4.d.ts]
@ -127,3 +141,12 @@ declare type DeepReadonlyFoo = {
};
declare var x1: DeepReadonly<Foo>;
declare var x1: DeepReadonlyFoo;
declare type Z = {
a: number;
};
declare type Clone<T> = {
[P in keyof (T & {})]: T[P];
};
declare type M = Clone<Z>;
declare var z1: Z;
declare var z1: Clone<Z>;

View file

@ -196,3 +196,34 @@ var x1: DeepReadonlyFoo;
>x1 : Symbol(x1, Decl(mappedTypes4.ts, 58, 3), Decl(mappedTypes4.ts, 59, 3))
>DeepReadonlyFoo : Symbol(DeepReadonlyFoo, Decl(mappedTypes4.ts, 50, 2))
// Repro from #13232
type Z = { a: number };
>Z : Symbol(Z, Decl(mappedTypes4.ts, 59, 24))
>a : Symbol(a, Decl(mappedTypes4.ts, 63, 10))
type Clone<T> = {
>Clone : Symbol(Clone, Decl(mappedTypes4.ts, 63, 23))
>T : Symbol(T, Decl(mappedTypes4.ts, 64, 11))
[P in keyof (T & {})]: T[P];
>P : Symbol(P, Decl(mappedTypes4.ts, 65, 3))
>T : Symbol(T, Decl(mappedTypes4.ts, 64, 11))
>T : Symbol(T, Decl(mappedTypes4.ts, 64, 11))
>P : Symbol(P, Decl(mappedTypes4.ts, 65, 3))
};
type M = Clone<Z>; // M should be { a: number }
>M : Symbol(M, Decl(mappedTypes4.ts, 66, 2))
>Clone : Symbol(Clone, Decl(mappedTypes4.ts, 63, 23))
>Z : Symbol(Z, Decl(mappedTypes4.ts, 59, 24))
var z1: Z;
>z1 : Symbol(z1, Decl(mappedTypes4.ts, 69, 3), Decl(mappedTypes4.ts, 70, 3))
>Z : Symbol(Z, Decl(mappedTypes4.ts, 59, 24))
var z1: Clone<Z>;
>z1 : Symbol(z1, Decl(mappedTypes4.ts, 69, 3), Decl(mappedTypes4.ts, 70, 3))
>Clone : Symbol(Clone, Decl(mappedTypes4.ts, 63, 23))
>Z : Symbol(Z, Decl(mappedTypes4.ts, 59, 24))

View file

@ -211,3 +211,34 @@ var x1: DeepReadonlyFoo;
>x1 : DeepReadonly<Foo>
>DeepReadonlyFoo : DeepReadonlyFoo
// Repro from #13232
type Z = { a: number };
>Z : Z
>a : number
type Clone<T> = {
>Clone : Clone<T>
>T : T
[P in keyof (T & {})]: T[P];
>P : P
>T : T
>T : T
>P : P
};
type M = Clone<Z>; // M should be { a: number }
>M : Clone<Z>
>Clone : Clone<T>
>Z : Z
var z1: Z;
>z1 : Z
>Z : Z
var z1: Clone<Z>;
>z1 : Z
>Clone : Clone<T>
>Z : Z

View file

@ -59,4 +59,15 @@ type DeepReadonlyFoo = {
};
var x1: DeepReadonly<Foo>;
var x1: DeepReadonlyFoo;
var x1: DeepReadonlyFoo;
// Repro from #13232
type Z = { a: number };
type Clone<T> = {
[P in keyof (T & {})]: T[P];
};
type M = Clone<Z>; // M should be { a: number }
var z1: Z;
var z1: Clone<Z>;