mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 02:33:53 -06:00
Merge pull request #13366 from Microsoft/fixMappedTypeCombinedMappers
Fix bug in recursive mapped type instantiation
This commit is contained in:
commit
f667b5cf50
@ -6558,6 +6558,12 @@ namespace ts {
|
||||
return mapper;
|
||||
}
|
||||
|
||||
function createReplacementMapper(source: Type, target: Type, baseMapper: TypeMapper) {
|
||||
const mapper: TypeMapper = t => t === source ? target : baseMapper(t);
|
||||
mapper.mappedTypes = baseMapper.mappedTypes;
|
||||
return mapper;
|
||||
}
|
||||
|
||||
function cloneTypeParameter(typeParameter: TypeParameter): TypeParameter {
|
||||
const result = <TypeParameter>createType(TypeFlags.TypeParameter);
|
||||
result.symbol = typeParameter.symbol;
|
||||
@ -6656,10 +6662,7 @@ namespace ts {
|
||||
if (typeVariable !== mappedTypeVariable) {
|
||||
return mapType(mappedTypeVariable, t => {
|
||||
if (isMappableType(t)) {
|
||||
const replacementMapper = createTypeMapper([typeVariable], [t]);
|
||||
const combinedMapper = mapper.mappedTypes && mapper.mappedTypes.length === 1 ? replacementMapper : combineTypeMappers(replacementMapper, mapper);
|
||||
combinedMapper.mappedTypes = mapper.mappedTypes;
|
||||
return instantiateMappedObjectType(type, combinedMapper);
|
||||
return instantiateMappedObjectType(type, createReplacementMapper(typeVariable, t, mapper));
|
||||
}
|
||||
return t;
|
||||
});
|
||||
|
||||
@ -0,0 +1,25 @@
|
||||
tests/cases/compiler/mappedTypeWithCombinedTypeMappers.ts(18,7): error TS2322: Type 'string' is not assignable to type '{ important: boolean; }'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/mappedTypeWithCombinedTypeMappers.ts (1 errors) ====
|
||||
// Repro from #13351
|
||||
|
||||
type Meta<T, A> = {
|
||||
readonly[P in keyof T]: {
|
||||
value: T[P];
|
||||
also: A;
|
||||
readonly children: Meta<T[P], A>;
|
||||
};
|
||||
}
|
||||
|
||||
interface Input {
|
||||
x: string;
|
||||
y: number;
|
||||
}
|
||||
|
||||
declare const output: Meta<Input, boolean>;
|
||||
|
||||
const shouldFail: { important: boolean } = output.x.children;
|
||||
~~~~~~~~~~
|
||||
!!! error TS2322: Type 'string' is not assignable to type '{ important: boolean; }'.
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
//// [mappedTypeWithCombinedTypeMappers.ts]
|
||||
// Repro from #13351
|
||||
|
||||
type Meta<T, A> = {
|
||||
readonly[P in keyof T]: {
|
||||
value: T[P];
|
||||
also: A;
|
||||
readonly children: Meta<T[P], A>;
|
||||
};
|
||||
}
|
||||
|
||||
interface Input {
|
||||
x: string;
|
||||
y: number;
|
||||
}
|
||||
|
||||
declare const output: Meta<Input, boolean>;
|
||||
|
||||
const shouldFail: { important: boolean } = output.x.children;
|
||||
|
||||
|
||||
//// [mappedTypeWithCombinedTypeMappers.js]
|
||||
// Repro from #13351
|
||||
var shouldFail = output.x.children;
|
||||
18
tests/cases/compiler/mappedTypeWithCombinedTypeMappers.ts
Normal file
18
tests/cases/compiler/mappedTypeWithCombinedTypeMappers.ts
Normal file
@ -0,0 +1,18 @@
|
||||
// Repro from #13351
|
||||
|
||||
type Meta<T, A> = {
|
||||
readonly[P in keyof T]: {
|
||||
value: T[P];
|
||||
also: A;
|
||||
readonly children: Meta<T[P], A>;
|
||||
};
|
||||
}
|
||||
|
||||
interface Input {
|
||||
x: string;
|
||||
y: number;
|
||||
}
|
||||
|
||||
declare const output: Meta<Input, boolean>;
|
||||
|
||||
const shouldFail: { important: boolean } = output.x.children;
|
||||
Loading…
x
Reference in New Issue
Block a user