diff --git a/tests/baselines/reference/keyofAndIndexedAccess.js b/tests/baselines/reference/keyofAndIndexedAccess.js index 3ad7bdd79f8..f5c2624fc40 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.js +++ b/tests/baselines/reference/keyofAndIndexedAccess.js @@ -463,6 +463,33 @@ let MyThingy: { [key in KeyTypes]: string[] }; function addToMyThingy(key: S) { MyThingy[key].push("a"); } + +// Repro from #13285 + +function updateIds, K extends string>( + obj: T, + idFields: K[], + idMapping: { [oldId: string]: string } +): Record { + for (const idField of idFields) { + const newId = idMapping[obj[idField]]; + if (newId) { + obj[idField] = newId; + } + } + return obj; +} + +// Repro from #13285 + +function updateIds2( + obj: T, + key: K, + stringMap: { [oldId: string]: string } +) { + var x = obj[key]; + stringMap[x]; // Should be OK. +} //// [keyofAndIndexedAccess.js] @@ -769,6 +796,22 @@ var MyThingy; function addToMyThingy(key) { MyThingy[key].push("a"); } +// Repro from #13285 +function updateIds(obj, idFields, idMapping) { + for (var _i = 0, idFields_1 = idFields; _i < idFields_1.length; _i++) { + var idField = idFields_1[_i]; + var newId = idMapping[obj[idField]]; + if (newId) { + obj[idField] = newId; + } + } + return obj; +} +// Repro from #13285 +function updateIds2(obj, key, stringMap) { + var x = obj[key]; + stringMap[x]; // Should be OK. +} //// [keyofAndIndexedAccess.d.ts] @@ -986,3 +1029,11 @@ declare let MyThingy: { [key in KeyTypes]: string[]; }; declare function addToMyThingy(key: S): void; +declare function updateIds, K extends string>(obj: T, idFields: K[], idMapping: { + [oldId: string]: string; +}): Record; +declare function updateIds2(obj: T, key: K, stringMap: { + [oldId: string]: string; +}): void; diff --git a/tests/baselines/reference/keyofAndIndexedAccess.symbols b/tests/baselines/reference/keyofAndIndexedAccess.symbols index fb8129a5301..a6eb03dd6e5 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.symbols +++ b/tests/baselines/reference/keyofAndIndexedAccess.symbols @@ -1699,3 +1699,83 @@ function addToMyThingy(key: S) { >push : Symbol(Array.push, Decl(lib.d.ts, --, --)) } +// Repro from #13285 + +function updateIds, K extends string>( +>updateIds : Symbol(updateIds, Decl(keyofAndIndexedAccess.ts, 463, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 467, 19)) +>Record : Symbol(Record, Decl(lib.d.ts, --, --)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 467, 47)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 467, 47)) + + obj: T, +>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 467, 66)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 467, 19)) + + idFields: K[], +>idFields : Symbol(idFields, Decl(keyofAndIndexedAccess.ts, 468, 11)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 467, 47)) + + idMapping: { [oldId: string]: string } +>idMapping : Symbol(idMapping, Decl(keyofAndIndexedAccess.ts, 469, 18)) +>oldId : Symbol(oldId, Decl(keyofAndIndexedAccess.ts, 470, 18)) + +): Record { +>Record : Symbol(Record, Decl(lib.d.ts, --, --)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 467, 47)) + + for (const idField of idFields) { +>idField : Symbol(idField, Decl(keyofAndIndexedAccess.ts, 472, 14)) +>idFields : Symbol(idFields, Decl(keyofAndIndexedAccess.ts, 468, 11)) + + const newId = idMapping[obj[idField]]; +>newId : Symbol(newId, Decl(keyofAndIndexedAccess.ts, 473, 13)) +>idMapping : Symbol(idMapping, Decl(keyofAndIndexedAccess.ts, 469, 18)) +>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 467, 66)) +>idField : Symbol(idField, Decl(keyofAndIndexedAccess.ts, 472, 14)) + + if (newId) { +>newId : Symbol(newId, Decl(keyofAndIndexedAccess.ts, 473, 13)) + + obj[idField] = newId; +>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 467, 66)) +>idField : Symbol(idField, Decl(keyofAndIndexedAccess.ts, 472, 14)) +>newId : Symbol(newId, Decl(keyofAndIndexedAccess.ts, 473, 13)) + } + } + return obj; +>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 467, 66)) +} + +// Repro from #13285 + +function updateIds2( +>updateIds2 : Symbol(updateIds2, Decl(keyofAndIndexedAccess.ts, 479, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 483, 20)) +>x : Symbol(x, Decl(keyofAndIndexedAccess.ts, 483, 33)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 483, 54)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 483, 20)) + + obj: T, +>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 483, 74)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 483, 20)) + + key: K, +>key : Symbol(key, Decl(keyofAndIndexedAccess.ts, 484, 11)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 483, 54)) + + stringMap: { [oldId: string]: string } +>stringMap : Symbol(stringMap, Decl(keyofAndIndexedAccess.ts, 485, 11)) +>oldId : Symbol(oldId, Decl(keyofAndIndexedAccess.ts, 486, 18)) + +) { + var x = obj[key]; +>x : Symbol(x, Decl(keyofAndIndexedAccess.ts, 488, 7)) +>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 483, 74)) +>key : Symbol(key, Decl(keyofAndIndexedAccess.ts, 484, 11)) + + stringMap[x]; // Should be OK. +>stringMap : Symbol(stringMap, Decl(keyofAndIndexedAccess.ts, 485, 11)) +>x : Symbol(x, Decl(keyofAndIndexedAccess.ts, 488, 7)) +} + diff --git a/tests/baselines/reference/keyofAndIndexedAccess.types b/tests/baselines/reference/keyofAndIndexedAccess.types index ccf0bde4e1c..920758c8beb 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.types +++ b/tests/baselines/reference/keyofAndIndexedAccess.types @@ -2013,3 +2013,89 @@ function addToMyThingy(key: S) { >"a" : "a" } +// Repro from #13285 + +function updateIds, K extends string>( +>updateIds : , K extends string>(obj: T, idFields: K[], idMapping: { [oldId: string]: string; }) => Record +>T : T +>Record : Record +>K : K +>K : K + + obj: T, +>obj : T +>T : T + + idFields: K[], +>idFields : K[] +>K : K + + idMapping: { [oldId: string]: string } +>idMapping : { [oldId: string]: string; } +>oldId : string + +): Record { +>Record : Record +>K : K + + for (const idField of idFields) { +>idField : K +>idFields : K[] + + const newId = idMapping[obj[idField]]; +>newId : { [oldId: string]: string; }[T[K]] +>idMapping[obj[idField]] : { [oldId: string]: string; }[T[K]] +>idMapping : { [oldId: string]: string; } +>obj[idField] : T[K] +>obj : T +>idField : K + + if (newId) { +>newId : { [oldId: string]: string; }[T[K]] + + obj[idField] = newId; +>obj[idField] = newId : { [oldId: string]: string; }[T[K]] +>obj[idField] : T[K] +>obj : T +>idField : K +>newId : { [oldId: string]: string; }[T[K]] + } + } + return obj; +>obj : T +} + +// Repro from #13285 + +function updateIds2( +>updateIds2 : (obj: T, key: K, stringMap: { [oldId: string]: string; }) => void +>T : T +>x : string +>K : K +>T : T + + obj: T, +>obj : T +>T : T + + key: K, +>key : K +>K : K + + stringMap: { [oldId: string]: string } +>stringMap : { [oldId: string]: string; } +>oldId : string + +) { + var x = obj[key]; +>x : T[K] +>obj[key] : T[K] +>obj : T +>key : K + + stringMap[x]; // Should be OK. +>stringMap[x] : { [oldId: string]: string; }[T[K]] +>stringMap : { [oldId: string]: string; } +>x : T[K] +} + diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts index 0ef73c736f4..770dc2ead98 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts @@ -464,3 +464,30 @@ let MyThingy: { [key in KeyTypes]: string[] }; function addToMyThingy(key: S) { MyThingy[key].push("a"); } + +// Repro from #13285 + +function updateIds, K extends string>( + obj: T, + idFields: K[], + idMapping: { [oldId: string]: string } +): Record { + for (const idField of idFields) { + const newId = idMapping[obj[idField]]; + if (newId) { + obj[idField] = newId; + } + } + return obj; +} + +// Repro from #13285 + +function updateIds2( + obj: T, + key: K, + stringMap: { [oldId: string]: string } +) { + var x = obj[key]; + stringMap[x]; // Should be OK. +}