Skip to content

Commit 81e8918

Browse files
committed
Add regression test
1 parent fc641fa commit 81e8918

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

tests/baselines/reference/keyofAndIndexedAccess.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,33 @@ let MyThingy: { [key in KeyTypes]: string[] };
463463
function addToMyThingy<S extends KeyTypes>(key: S) {
464464
MyThingy[key].push("a");
465465
}
466+
467+
// Repro from #13285
468+
469+
function updateIds<T extends Record<K, string>, K extends string>(
470+
obj: T,
471+
idFields: K[],
472+
idMapping: { [oldId: string]: string }
473+
): Record<K, string> {
474+
for (const idField of idFields) {
475+
const newId = idMapping[obj[idField]];
476+
if (newId) {
477+
obj[idField] = newId;
478+
}
479+
}
480+
return obj;
481+
}
482+
483+
// Repro from #13285
484+
485+
function updateIds2<T extends { [x: string]: string }, K extends keyof T>(
486+
obj: T,
487+
key: K,
488+
stringMap: { [oldId: string]: string }
489+
) {
490+
var x = obj[key];
491+
stringMap[x]; // Should be OK.
492+
}
466493

467494

468495
//// [keyofAndIndexedAccess.js]
@@ -769,6 +796,22 @@ var MyThingy;
769796
function addToMyThingy(key) {
770797
MyThingy[key].push("a");
771798
}
799+
// Repro from #13285
800+
function updateIds(obj, idFields, idMapping) {
801+
for (var _i = 0, idFields_1 = idFields; _i < idFields_1.length; _i++) {
802+
var idField = idFields_1[_i];
803+
var newId = idMapping[obj[idField]];
804+
if (newId) {
805+
obj[idField] = newId;
806+
}
807+
}
808+
return obj;
809+
}
810+
// Repro from #13285
811+
function updateIds2(obj, key, stringMap) {
812+
var x = obj[key];
813+
stringMap[x]; // Should be OK.
814+
}
772815

773816

774817
//// [keyofAndIndexedAccess.d.ts]
@@ -986,3 +1029,11 @@ declare let MyThingy: {
9861029
[key in KeyTypes]: string[];
9871030
};
9881031
declare function addToMyThingy<S extends KeyTypes>(key: S): void;
1032+
declare function updateIds<T extends Record<K, string>, K extends string>(obj: T, idFields: K[], idMapping: {
1033+
[oldId: string]: string;
1034+
}): Record<K, string>;
1035+
declare function updateIds2<T extends {
1036+
[x: string]: string;
1037+
}, K extends keyof T>(obj: T, key: K, stringMap: {
1038+
[oldId: string]: string;
1039+
}): void;

tests/baselines/reference/keyofAndIndexedAccess.symbols

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,3 +1699,83 @@ function addToMyThingy<S extends KeyTypes>(key: S) {
16991699
>push : Symbol(Array.push, Decl(lib.d.ts, --, --))
17001700
}
17011701

1702+
// Repro from #13285
1703+
1704+
function updateIds<T extends Record<K, string>, K extends string>(
1705+
>updateIds : Symbol(updateIds, Decl(keyofAndIndexedAccess.ts, 463, 1))
1706+
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 467, 19))
1707+
>Record : Symbol(Record, Decl(lib.d.ts, --, --))
1708+
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 467, 47))
1709+
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 467, 47))
1710+
1711+
obj: T,
1712+
>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 467, 66))
1713+
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 467, 19))
1714+
1715+
idFields: K[],
1716+
>idFields : Symbol(idFields, Decl(keyofAndIndexedAccess.ts, 468, 11))
1717+
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 467, 47))
1718+
1719+
idMapping: { [oldId: string]: string }
1720+
>idMapping : Symbol(idMapping, Decl(keyofAndIndexedAccess.ts, 469, 18))
1721+
>oldId : Symbol(oldId, Decl(keyofAndIndexedAccess.ts, 470, 18))
1722+
1723+
): Record<K, string> {
1724+
>Record : Symbol(Record, Decl(lib.d.ts, --, --))
1725+
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 467, 47))
1726+
1727+
for (const idField of idFields) {
1728+
>idField : Symbol(idField, Decl(keyofAndIndexedAccess.ts, 472, 14))
1729+
>idFields : Symbol(idFields, Decl(keyofAndIndexedAccess.ts, 468, 11))
1730+
1731+
const newId = idMapping[obj[idField]];
1732+
>newId : Symbol(newId, Decl(keyofAndIndexedAccess.ts, 473, 13))
1733+
>idMapping : Symbol(idMapping, Decl(keyofAndIndexedAccess.ts, 469, 18))
1734+
>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 467, 66))
1735+
>idField : Symbol(idField, Decl(keyofAndIndexedAccess.ts, 472, 14))
1736+
1737+
if (newId) {
1738+
>newId : Symbol(newId, Decl(keyofAndIndexedAccess.ts, 473, 13))
1739+
1740+
obj[idField] = newId;
1741+
>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 467, 66))
1742+
>idField : Symbol(idField, Decl(keyofAndIndexedAccess.ts, 472, 14))
1743+
>newId : Symbol(newId, Decl(keyofAndIndexedAccess.ts, 473, 13))
1744+
}
1745+
}
1746+
return obj;
1747+
>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 467, 66))
1748+
}
1749+
1750+
// Repro from #13285
1751+
1752+
function updateIds2<T extends { [x: string]: string }, K extends keyof T>(
1753+
>updateIds2 : Symbol(updateIds2, Decl(keyofAndIndexedAccess.ts, 479, 1))
1754+
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 483, 20))
1755+
>x : Symbol(x, Decl(keyofAndIndexedAccess.ts, 483, 33))
1756+
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 483, 54))
1757+
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 483, 20))
1758+
1759+
obj: T,
1760+
>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 483, 74))
1761+
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 483, 20))
1762+
1763+
key: K,
1764+
>key : Symbol(key, Decl(keyofAndIndexedAccess.ts, 484, 11))
1765+
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 483, 54))
1766+
1767+
stringMap: { [oldId: string]: string }
1768+
>stringMap : Symbol(stringMap, Decl(keyofAndIndexedAccess.ts, 485, 11))
1769+
>oldId : Symbol(oldId, Decl(keyofAndIndexedAccess.ts, 486, 18))
1770+
1771+
) {
1772+
var x = obj[key];
1773+
>x : Symbol(x, Decl(keyofAndIndexedAccess.ts, 488, 7))
1774+
>obj : Symbol(obj, Decl(keyofAndIndexedAccess.ts, 483, 74))
1775+
>key : Symbol(key, Decl(keyofAndIndexedAccess.ts, 484, 11))
1776+
1777+
stringMap[x]; // Should be OK.
1778+
>stringMap : Symbol(stringMap, Decl(keyofAndIndexedAccess.ts, 485, 11))
1779+
>x : Symbol(x, Decl(keyofAndIndexedAccess.ts, 488, 7))
1780+
}
1781+

tests/baselines/reference/keyofAndIndexedAccess.types

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,3 +2013,89 @@ function addToMyThingy<S extends KeyTypes>(key: S) {
20132013
>"a" : "a"
20142014
}
20152015

2016+
// Repro from #13285
2017+
2018+
function updateIds<T extends Record<K, string>, K extends string>(
2019+
>updateIds : <T extends Record<K, string>, K extends string>(obj: T, idFields: K[], idMapping: { [oldId: string]: string; }) => Record<K, string>
2020+
>T : T
2021+
>Record : Record<K, T>
2022+
>K : K
2023+
>K : K
2024+
2025+
obj: T,
2026+
>obj : T
2027+
>T : T
2028+
2029+
idFields: K[],
2030+
>idFields : K[]
2031+
>K : K
2032+
2033+
idMapping: { [oldId: string]: string }
2034+
>idMapping : { [oldId: string]: string; }
2035+
>oldId : string
2036+
2037+
): Record<K, string> {
2038+
>Record : Record<K, T>
2039+
>K : K
2040+
2041+
for (const idField of idFields) {
2042+
>idField : K
2043+
>idFields : K[]
2044+
2045+
const newId = idMapping[obj[idField]];
2046+
>newId : { [oldId: string]: string; }[T[K]]
2047+
>idMapping[obj[idField]] : { [oldId: string]: string; }[T[K]]
2048+
>idMapping : { [oldId: string]: string; }
2049+
>obj[idField] : T[K]
2050+
>obj : T
2051+
>idField : K
2052+
2053+
if (newId) {
2054+
>newId : { [oldId: string]: string; }[T[K]]
2055+
2056+
obj[idField] = newId;
2057+
>obj[idField] = newId : { [oldId: string]: string; }[T[K]]
2058+
>obj[idField] : T[K]
2059+
>obj : T
2060+
>idField : K
2061+
>newId : { [oldId: string]: string; }[T[K]]
2062+
}
2063+
}
2064+
return obj;
2065+
>obj : T
2066+
}
2067+
2068+
// Repro from #13285
2069+
2070+
function updateIds2<T extends { [x: string]: string }, K extends keyof T>(
2071+
>updateIds2 : <T extends { [x: string]: string; }, K extends keyof T>(obj: T, key: K, stringMap: { [oldId: string]: string; }) => void
2072+
>T : T
2073+
>x : string
2074+
>K : K
2075+
>T : T
2076+
2077+
obj: T,
2078+
>obj : T
2079+
>T : T
2080+
2081+
key: K,
2082+
>key : K
2083+
>K : K
2084+
2085+
stringMap: { [oldId: string]: string }
2086+
>stringMap : { [oldId: string]: string; }
2087+
>oldId : string
2088+
2089+
) {
2090+
var x = obj[key];
2091+
>x : T[K]
2092+
>obj[key] : T[K]
2093+
>obj : T
2094+
>key : K
2095+
2096+
stringMap[x]; // Should be OK.
2097+
>stringMap[x] : { [oldId: string]: string; }[T[K]]
2098+
>stringMap : { [oldId: string]: string; }
2099+
>x : T[K]
2100+
}
2101+

tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,30 @@ let MyThingy: { [key in KeyTypes]: string[] };
464464
function addToMyThingy<S extends KeyTypes>(key: S) {
465465
MyThingy[key].push("a");
466466
}
467+
468+
// Repro from #13285
469+
470+
function updateIds<T extends Record<K, string>, K extends string>(
471+
obj: T,
472+
idFields: K[],
473+
idMapping: { [oldId: string]: string }
474+
): Record<K, string> {
475+
for (const idField of idFields) {
476+
const newId = idMapping[obj[idField]];
477+
if (newId) {
478+
obj[idField] = newId;
479+
}
480+
}
481+
return obj;
482+
}
483+
484+
// Repro from #13285
485+
486+
function updateIds2<T extends { [x: string]: string }, K extends keyof T>(
487+
obj: T,
488+
key: K,
489+
stringMap: { [oldId: string]: string }
490+
) {
491+
var x = obj[key];
492+
stringMap[x]; // Should be OK.
493+
}

0 commit comments

Comments
 (0)