|
| 1 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(4,5): error TS2322: Type '"x"' is not assignable to type 'number'. |
| 2 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(6,5): error TS2322: Type '2' is not assignable to type '0 | 1'. |
| 3 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(7,5): error TS2322: Type '"x"' is not assignable to type '0 | 1'. |
| 4 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(8,5): error TS2322: Type '1' is not assignable to type 'never'. |
| 5 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(9,5): error TS2322: Type '2' is not assignable to type 'never'. |
| 6 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(10,5): error TS2322: Type '"x"' is not assignable to type 'never'. |
| 7 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(14,5): error TS2739: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y |
| 8 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(15,5): error TS2322: Type 'T' is not assignable to type '{ x: number; y: number; }'. |
| 9 | + Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y |
| 10 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(18,5): error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'T'. |
| 11 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(19,5): error TS2322: Type '{ [key: string]: number; }' is not assignable to type 'T'. |
| 12 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(26,7): error TS2339: Property 'x' does not exist on type 'T'. |
| 13 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(27,5): error TS2322: Type '1' is not assignable to type 'T[keyof T]'. |
| 14 | + Type '1' is not assignable to type 'T[string] & T[number]'. |
| 15 | + Type '1' is not assignable to type 'T[string]'. |
| 16 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(31,5): error TS2322: Type '{ [key: string]: number; }' is not assignable to type '{ [P in K]: number; }'. |
| 17 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(45,3): error TS7017: Element implicitly has an 'any' type because type 'Item' has no index signature. |
| 18 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(46,3): error TS2322: Type '123' is not assignable to type 'string & number'. |
| 19 | + Type '123' is not assignable to type 'string'. |
| 20 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(47,3): error TS2322: Type '123' is not assignable to type 'T[keyof T]'. |
| 21 | + Type '123' is not assignable to type 'T["a"] & T["b"]'. |
| 22 | + Type '123' is not assignable to type 'T["a"]'. |
| 23 | + Type '123' is not assignable to type 'string'. |
| 24 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(48,3): error TS2322: Type '123' is not assignable to type 'T[K]'. |
| 25 | + Type '123' is not assignable to type 'T["a"] & T["b"]'. |
| 26 | + Type '123' is not assignable to type 'T["a"]'. |
| 27 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(60,7): error TS2339: Property 'foo' does not exist on type 'T'. |
| 28 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(61,3): error TS2536: Type 'string' cannot be used to index type 'T'. |
| 29 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(62,3): error TS2322: Type '123' is not assignable to type 'T[keyof T]'. |
| 30 | + Type '123' is not assignable to type 'T[string]'. |
| 31 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(63,3): error TS2322: Type '123' is not assignable to type 'T[K]'. |
| 32 | + Type '123' is not assignable to type 'T[string]'. |
| 33 | +tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(103,5): error TS2322: Type '123' is not assignable to type 'Type[K]'. |
| 34 | + Type '123' is not assignable to type '123 & "some string"'. |
| 35 | + Type '123' is not assignable to type '"some string"'. |
| 36 | + |
| 37 | + |
| 38 | +==== tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts (22 errors) ==== |
| 39 | + function f1(obj: { a: number, b: 0 | 1, c: string }, k0: 'a', k1: 'a' | 'b', k2: 'a' | 'b' | 'c') { |
| 40 | + obj[k0] = 1; |
| 41 | + obj[k0] = 2; |
| 42 | + obj[k0] = 'x'; // Error |
| 43 | + ~~~~~~~ |
| 44 | +!!! error TS2322: Type '"x"' is not assignable to type 'number'. |
| 45 | + obj[k1] = 1; |
| 46 | + obj[k1] = 2; // Error |
| 47 | + ~~~~~~~ |
| 48 | +!!! error TS2322: Type '2' is not assignable to type '0 | 1'. |
| 49 | + obj[k1] = 'x'; // Error |
| 50 | + ~~~~~~~ |
| 51 | +!!! error TS2322: Type '"x"' is not assignable to type '0 | 1'. |
| 52 | + obj[k2] = 1; // Error |
| 53 | + ~~~~~~~ |
| 54 | +!!! error TS2322: Type '1' is not assignable to type 'never'. |
| 55 | + obj[k2] = 2; // Error |
| 56 | + ~~~~~~~ |
| 57 | +!!! error TS2322: Type '2' is not assignable to type 'never'. |
| 58 | + obj[k2] = 'x'; // Error |
| 59 | + ~~~~~~~ |
| 60 | +!!! error TS2322: Type '"x"' is not assignable to type 'never'. |
| 61 | + } |
| 62 | + |
| 63 | + function f2<T extends { [key: string]: number }>(a: { x: number, y: number }, b: { [key: string]: number }, c: T, k: keyof T) { |
| 64 | + a = b; // Error, index signature in source doesn't imply properties are present |
| 65 | + ~ |
| 66 | +!!! error TS2739: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y |
| 67 | + a = c; // Error, index signature in source doesn't imply properties are present |
| 68 | + ~ |
| 69 | +!!! error TS2322: Type 'T' is not assignable to type '{ x: number; y: number; }'. |
| 70 | +!!! error TS2322: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y |
| 71 | + b = a; |
| 72 | + b = c; |
| 73 | + c = a; // Error, constraint on target doesn't imply any properties or signatures |
| 74 | + ~ |
| 75 | +!!! error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'T'. |
| 76 | + c = b; // Error, constraint on target doesn't imply any properties or signatures |
| 77 | + ~ |
| 78 | +!!! error TS2322: Type '{ [key: string]: number; }' is not assignable to type 'T'. |
| 79 | + a.x; |
| 80 | + b.x; |
| 81 | + c.x; |
| 82 | + c[k]; |
| 83 | + a.x = 1; |
| 84 | + b.x = 1; |
| 85 | + c.x = 1; // Error, cannot write to index signature through constraint |
| 86 | + ~ |
| 87 | +!!! error TS2339: Property 'x' does not exist on type 'T'. |
| 88 | + c[k] = 1; // Error, cannot write to index signature through constraint |
| 89 | + ~~~~ |
| 90 | +!!! error TS2322: Type '1' is not assignable to type 'T[keyof T]'. |
| 91 | +!!! error TS2322: Type '1' is not assignable to type 'T[string] & T[number]'. |
| 92 | +!!! error TS2322: Type '1' is not assignable to type 'T[string]'. |
| 93 | + } |
| 94 | + |
| 95 | + function f3<K extends string>(a: { [P in K]: number }, b: { [key: string]: number }, k: K) { |
| 96 | + a = b; // Error, index signature doesn't imply properties are present |
| 97 | + ~ |
| 98 | +!!! error TS2322: Type '{ [key: string]: number; }' is not assignable to type '{ [P in K]: number; }'. |
| 99 | + b = a; |
| 100 | + a[k]; |
| 101 | + a[k] = 1; |
| 102 | + } |
| 103 | + |
| 104 | + function f4<K extends string>(a: { [key: string]: number }[K], b: number) { |
| 105 | + a = b; |
| 106 | + b = a; |
| 107 | + } |
| 108 | + |
| 109 | + type Item = { a: string, b: number }; |
| 110 | + |
| 111 | + function f10<T extends Item, K extends keyof T>(obj: T, k1: string, k2: keyof Item, k3: keyof T, k4: K) { |
| 112 | + obj[k1] = 123; // Error |
| 113 | + ~~~~~~~ |
| 114 | +!!! error TS7017: Element implicitly has an 'any' type because type 'Item' has no index signature. |
| 115 | + obj[k2] = 123; // Error |
| 116 | + ~~~~~~~ |
| 117 | +!!! error TS2322: Type '123' is not assignable to type 'string & number'. |
| 118 | +!!! error TS2322: Type '123' is not assignable to type 'string'. |
| 119 | + obj[k3] = 123; // Error |
| 120 | + ~~~~~~~ |
| 121 | +!!! error TS2322: Type '123' is not assignable to type 'T[keyof T]'. |
| 122 | +!!! error TS2322: Type '123' is not assignable to type 'T["a"] & T["b"]'. |
| 123 | +!!! error TS2322: Type '123' is not assignable to type 'T["a"]'. |
| 124 | +!!! error TS2322: Type '123' is not assignable to type 'string'. |
| 125 | + obj[k4] = 123; // Error |
| 126 | + ~~~~~~~ |
| 127 | +!!! error TS2322: Type '123' is not assignable to type 'T[K]'. |
| 128 | +!!! error TS2322: Type '123' is not assignable to type 'T["a"] & T["b"]'. |
| 129 | +!!! error TS2322: Type '123' is not assignable to type 'T["a"]'. |
| 130 | + } |
| 131 | + |
| 132 | + type Dict = Record<string, number>; |
| 133 | + |
| 134 | + function f11<K extends keyof Dict>(obj: Dict, k1: keyof Dict, k2: K) { |
| 135 | + obj.foo = 123; |
| 136 | + obj[k1] = 123; |
| 137 | + obj[k2] = 123; |
| 138 | + } |
| 139 | + |
| 140 | + function f12<T extends Readonly<Dict>, K extends keyof T>(obj: T, k1: keyof Dict, k2: keyof T, k3: K) { |
| 141 | + obj.foo = 123; // Error |
| 142 | + ~~~ |
| 143 | +!!! error TS2339: Property 'foo' does not exist on type 'T'. |
| 144 | + obj[k1] = 123; // Error |
| 145 | + ~~~~~~~ |
| 146 | +!!! error TS2536: Type 'string' cannot be used to index type 'T'. |
| 147 | + obj[k2] = 123; // Error |
| 148 | + ~~~~~~~ |
| 149 | +!!! error TS2322: Type '123' is not assignable to type 'T[keyof T]'. |
| 150 | +!!! error TS2322: Type '123' is not assignable to type 'T[string]'. |
| 151 | + obj[k3] = 123; // Error |
| 152 | + ~~~~~~~ |
| 153 | +!!! error TS2322: Type '123' is not assignable to type 'T[K]'. |
| 154 | +!!! error TS2322: Type '123' is not assignable to type 'T[string]'. |
| 155 | + } |
| 156 | + |
| 157 | + // Repro from #27895 |
| 158 | + |
| 159 | + export interface Entity { |
| 160 | + id: number | string; |
| 161 | + } |
| 162 | + |
| 163 | + export type IdOf<E extends Entity> = E['id']; |
| 164 | + |
| 165 | + export interface EntityState<E extends Entity> { |
| 166 | + ids: IdOf<E>[]; |
| 167 | + entities: { [key: string]: E, [key: number]: E }; |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + export function getAllEntities<E extends Entity>(state: EntityState<E>): E[] { |
| 172 | + const { ids, entities } = state; |
| 173 | + return ids.map(id => entities[id]); |
| 174 | + } |
| 175 | + |
| 176 | + export function getEntity<E extends Entity>(id: IdOf<E>, state: EntityState<E>): E | undefined { |
| 177 | + const { ids, entities } = state; |
| 178 | + |
| 179 | + if (!ids.includes(id)) { |
| 180 | + return undefined; |
| 181 | + } |
| 182 | + |
| 183 | + return entities[id]; |
| 184 | + } |
| 185 | + |
| 186 | + // Repro from #30603 |
| 187 | + |
| 188 | + interface Type { |
| 189 | + a: 123; |
| 190 | + b: "some string"; |
| 191 | + } |
| 192 | + |
| 193 | + function get123<K extends keyof Type>(): Type[K] { |
| 194 | + return 123; // Error |
| 195 | + ~~~~~~~~~~~ |
| 196 | +!!! error TS2322: Type '123' is not assignable to type 'Type[K]'. |
| 197 | +!!! error TS2322: Type '123' is not assignable to type '123 & "some string"'. |
| 198 | +!!! error TS2322: Type '123' is not assignable to type '"some string"'. |
| 199 | + } |
| 200 | + |
0 commit comments