Skip to content

fix(47794): Fix incorrect substitution on index access on generic mapped type with key remapping #47837

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15567,8 +15567,10 @@ namespace ts {
}

function substituteIndexedMappedType(objectType: MappedType, index: Type) {
const mapper = createTypeMapper([getTypeParameterFromMappedType(objectType)], [index]);
const templateMapper = combineTypeMappers(objectType.mapper, mapper);
let templateMapper = objectType.mapper;
if (!objectType.declaration.nameType) {
templateMapper = combineTypeMappers(templateMapper, createTypeMapper([getTypeParameterFromMappedType(objectType)], [index]));
}
return instantiateType(getTemplateTypeFromMappedType(objectType), templateMapper);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [indexAccessOnGenericMappedTypeWithKeyRemapping.ts]
type Foo<T extends string> = {
[RemappedT in T as `get${RemappedT}`]: RemappedT;
};
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`];


//// [indexAccessOnGenericMappedTypeWithKeyRemapping.js]
var get = function (t, foo) { return foo["get".concat(t)]; };
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/indexAccessOnGenericMappedTypeWithKeyRemapping.ts ===
type Foo<T extends string> = {
>Foo : Symbol(Foo, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 0, 0))
>T : Symbol(T, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 0, 9))

[RemappedT in T as `get${RemappedT}`]: RemappedT;
>RemappedT : Symbol(RemappedT, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 1, 5))
>T : Symbol(T, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 0, 9))
>RemappedT : Symbol(RemappedT, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 1, 5))
>RemappedT : Symbol(RemappedT, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 1, 5))

};
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`];
>get : Symbol(get, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 3, 5))
>T : Symbol(T, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 3, 13))
>t : Symbol(t, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 3, 31))
>T : Symbol(T, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 3, 13))
>foo : Symbol(foo, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 3, 36))
>Foo : Symbol(Foo, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 0, 0))
>T : Symbol(T, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 3, 13))
>T : Symbol(T, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 3, 13))
>foo : Symbol(foo, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 3, 36))
>t : Symbol(t, Decl(indexAccessOnGenericMappedTypeWithKeyRemapping.ts, 3, 31))

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/indexAccessOnGenericMappedTypeWithKeyRemapping.ts ===
type Foo<T extends string> = {
>Foo : Foo<T>

[RemappedT in T as `get${RemappedT}`]: RemappedT;
};
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`];
>get : <T extends string>(t: T, foo: Foo<T>) => T
><T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`] : <T extends string>(t: T, foo: Foo<T>) => T
>t : T
>foo : Foo<T>
>foo[`get${t}`] : Foo<T>[`get${T}`]
>foo : Foo<T>
>`get${t}` : `get${T}`
>t : T

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Foo<T extends string> = {
[RemappedT in T as `get${RemappedT}`]: RemappedT;
};
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`];