From 739acee1c95d32ed4eca274ea90b47a32adc1742 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 4 Apr 2019 16:30:26 -0700 Subject: [PATCH] Add tests --- .../types/keyof/keyofAndIndexedAccess2.ts | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts new file mode 100644 index 00000000000..ee724dc1251 --- /dev/null +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts @@ -0,0 +1,107 @@ +// @strict: true +// @target: esnext + +function f1(obj: { a: number, b: 0 | 1, c: string }, k0: 'a', k1: 'a' | 'b', k2: 'a' | 'b' | 'c') { + obj[k0] = 1; + obj[k0] = 2; + obj[k0] = 'x'; // Error + obj[k1] = 1; + obj[k1] = 2; // Error + obj[k1] = 'x'; // Error + obj[k2] = 1; // Error + obj[k2] = 2; // Error + obj[k2] = 'x'; // Error +} + +function f2(a: { x: number, y: number }, b: { [key: string]: number }, c: T, k: keyof T) { + a = b; // Error, index signature in source doesn't imply properties are present + a = c; // Error, index signature in source doesn't imply properties are present + b = a; + b = c; + c = a; // Error, constraint on target doesn't imply any properties or signatures + c = b; // Error, constraint on target doesn't imply any properties or signatures + a.x; + b.x; + c.x; + c[k]; + a.x = 1; + b.x = 1; + c.x = 1; // Error, cannot write to index signature through constraint + c[k] = 1; // Error, cannot write to index signature through constraint +} + +function f3(a: { [P in K]: number }, b: { [key: string]: number }, k: K) { + a = b; // Error, index signature doesn't imply properties are present + b = a; + a[k]; + a[k] = 1; +} + +function f4(a: { [key: string]: number }[K], b: number) { + a = b; + b = a; +} + +type Item = { a: string, b: number }; + +function f10(obj: T, k1: string, k2: keyof Item, k3: keyof T, k4: K) { + obj[k1] = 123; // Error + obj[k2] = 123; // Error + obj[k3] = 123; // Error + obj[k4] = 123; // Error +} + +type Dict = Record; + +function f11(obj: Dict, k1: keyof Dict, k2: K) { + obj.foo = 123; + obj[k1] = 123; + obj[k2] = 123; +} + +function f12, K extends keyof T>(obj: T, k1: keyof Dict, k2: keyof T, k3: K) { + obj.foo = 123; // Error + obj[k1] = 123; // Error + obj[k2] = 123; // Error + obj[k3] = 123; // Error +} + +// Repro from #27895 + +export interface Entity { + id: number | string; +} + +export type IdOf = E['id']; + +export interface EntityState { + ids: IdOf[]; + entities: { [key: string]: E, [key: number]: E }; +} + + +export function getAllEntities(state: EntityState): E[] { + const { ids, entities } = state; + return ids.map(id => entities[id]); +} + +export function getEntity(id: IdOf, state: EntityState): E | undefined { + const { ids, entities } = state; + + if (!ids.includes(id)) { + return undefined; + } + + return entities[id]; +} + +// Repro from #30603 + +interface Type { + a: 123; + b: "some string"; +} + +function get123(): Type[K] { + return 123; // Error +}