|
| 1 | +// @strict: true |
| 2 | +// @target: esnext |
| 3 | + |
| 4 | +function f1(obj: { a: number, b: 0 | 1, c: string }, k0: 'a', k1: 'a' | 'b', k2: 'a' | 'b' | 'c') { |
| 5 | + obj[k0] = 1; |
| 6 | + obj[k0] = 2; |
| 7 | + obj[k0] = 'x'; // Error |
| 8 | + obj[k1] = 1; |
| 9 | + obj[k1] = 2; // Error |
| 10 | + obj[k1] = 'x'; // Error |
| 11 | + obj[k2] = 1; // Error |
| 12 | + obj[k2] = 2; // Error |
| 13 | + obj[k2] = 'x'; // Error |
| 14 | +} |
| 15 | + |
| 16 | +function f2<T extends { [key: string]: number }>(a: { x: number, y: number }, b: { [key: string]: number }, c: T, k: keyof T) { |
| 17 | + a = b; // Error, index signature in source doesn't imply properties are present |
| 18 | + a = c; // Error, index signature in source doesn't imply properties are present |
| 19 | + b = a; |
| 20 | + b = c; |
| 21 | + c = a; // Error, constraint on target doesn't imply any properties or signatures |
| 22 | + c = b; // Error, constraint on target doesn't imply any properties or signatures |
| 23 | + a.x; |
| 24 | + b.x; |
| 25 | + c.x; |
| 26 | + c[k]; |
| 27 | + a.x = 1; |
| 28 | + b.x = 1; |
| 29 | + c.x = 1; // Error, cannot write to index signature through constraint |
| 30 | + c[k] = 1; // Error, cannot write to index signature through constraint |
| 31 | +} |
| 32 | + |
| 33 | +function f3<K extends string>(a: { [P in K]: number }, b: { [key: string]: number }, k: K) { |
| 34 | + a = b; // Error, index signature doesn't imply properties are present |
| 35 | + b = a; |
| 36 | + a[k]; |
| 37 | + a[k] = 1; |
| 38 | +} |
| 39 | + |
| 40 | +function f4<K extends string>(a: { [key: string]: number }[K], b: number) { |
| 41 | + a = b; |
| 42 | + b = a; |
| 43 | +} |
| 44 | + |
| 45 | +type Item = { a: string, b: number }; |
| 46 | + |
| 47 | +function f10<T extends Item, K extends keyof T>(obj: T, k1: string, k2: keyof Item, k3: keyof T, k4: K) { |
| 48 | + obj[k1] = 123; // Error |
| 49 | + obj[k2] = 123; // Error |
| 50 | + obj[k3] = 123; // Error |
| 51 | + obj[k4] = 123; // Error |
| 52 | +} |
| 53 | + |
| 54 | +type Dict = Record<string, number>; |
| 55 | + |
| 56 | +function f11<K extends keyof Dict>(obj: Dict, k1: keyof Dict, k2: K) { |
| 57 | + obj.foo = 123; |
| 58 | + obj[k1] = 123; |
| 59 | + obj[k2] = 123; |
| 60 | +} |
| 61 | + |
| 62 | +function f12<T extends Readonly<Dict>, K extends keyof T>(obj: T, k1: keyof Dict, k2: keyof T, k3: K) { |
| 63 | + obj.foo = 123; // Error |
| 64 | + obj[k1] = 123; // Error |
| 65 | + obj[k2] = 123; // Error |
| 66 | + obj[k3] = 123; // Error |
| 67 | +} |
| 68 | + |
| 69 | +// Repro from #27895 |
| 70 | + |
| 71 | +export interface Entity { |
| 72 | + id: number | string; |
| 73 | +} |
| 74 | + |
| 75 | +export type IdOf<E extends Entity> = E['id']; |
| 76 | + |
| 77 | +export interface EntityState<E extends Entity> { |
| 78 | + ids: IdOf<E>[]; |
| 79 | + entities: { [key: string]: E, [key: number]: E }; |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +export function getAllEntities<E extends Entity>(state: EntityState<E>): E[] { |
| 84 | + const { ids, entities } = state; |
| 85 | + return ids.map(id => entities[id]); |
| 86 | +} |
| 87 | + |
| 88 | +export function getEntity<E extends Entity>(id: IdOf<E>, state: EntityState<E>): E | undefined { |
| 89 | + const { ids, entities } = state; |
| 90 | + |
| 91 | + if (!ids.includes(id)) { |
| 92 | + return undefined; |
| 93 | + } |
| 94 | + |
| 95 | + return entities[id]; |
| 96 | +} |
| 97 | + |
| 98 | +// Repro from #30603 |
| 99 | + |
| 100 | +interface Type { |
| 101 | + a: 123; |
| 102 | + b: "some string"; |
| 103 | +} |
| 104 | + |
| 105 | +function get123<K extends keyof Type>(): Type[K] { |
| 106 | + return 123; // Error |
| 107 | +} |
0 commit comments