1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(4,5): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(6,5): error TS2322: Type '2' is not assignable to type '0 | 1'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(7,5): error TS2322: Type '"x"' is not assignable to type '0 | 1'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(8,5): error TS2322: Type 'number' is not assignable to type 'never'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(9,5): error TS2322: Type 'number' is not assignable to type 'never'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(10,5): error TS2322: Type 'string' is not assignable to type 'never'.
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
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(15,5): error TS2322: Type 'T' is not assignable to type '{ x: number; y: number; }'.
Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(18,5): error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'T'.
'{ x: number; y: number; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{ [key: string]: number; }'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(19,5): error TS2322: Type '{ [key: string]: number; }' is not assignable to type 'T'.
'{ [key: string]: number; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{ [key: string]: number; }'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(26,7): error TS2339: Property 'x' does not exist on type 'T'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(27,5): error TS2322: Type 'number' is not assignable to type 'T[keyof T]'.
'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(31,5): error TS2322: Type '{ [key: string]: number; }' is not assignable to type '{ [P in K]: number; }'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(38,5): error TS2322: Type '{ [x: string]: number; }' is not assignable to type '{ [P in K]: number; }'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(50,3): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Item'.
No index signature with a parameter of type 'string' was found on type 'Item'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(51,3): error TS2322: Type 'number' is not assignable to type 'never'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(52,3): error TS2322: Type 'number' is not assignable to type 'T[keyof T]'.
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'number'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(53,3): error TS2322: Type 'number' is not assignable to type 'T[K]'.
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'number'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(65,7): error TS2339: Property 'foo' does not exist on type 'T'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(66,3): error TS2536: Type 'string' cannot be used to index type 'T'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(67,3): error TS2322: Type 'number' is not assignable to type 'T[keyof T]'.
'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(68,3): error TS2322: Type 'number' is not assignable to type 'T[K]'.
'number' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'number'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS2322: Type '123' is not assignable to type 'Type[K]'.
Type 'number' is not assignable to type 'never'.
==== tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts (23 errors) ====
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
~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
obj[k1] = 1;
obj[k1] = 2; // Error
~~~~~~~
!!! error TS2322: Type '2' is not assignable to type '0 | 1'.
obj[k1] = 'x'; // Error
~~~~~~~
!!! error TS2322: Type '"x"' is not assignable to type '0 | 1'.
obj[k2] = 1; // Error
~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'never'.
obj[k2] = 2; // Error
~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'never'.
obj[k2] = 'x'; // Error
~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'never'.
}
function f2<T extends { [key: string]: number }>(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
~
!!! error TS2739: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
a = c; // Error, index signature in source doesn't imply properties are present
~
!!! error TS2322: Type 'T' is not assignable to type '{ x: number; y: number; }'.
!!! error TS2322: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
b = a;
b = c;
c = a; // Error, constraint on target doesn't imply any properties or signatures
~
!!! error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'T'.
!!! error TS2322: '{ x: number; y: number; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{ [key: string]: number; }'.
c = b; // Error, constraint on target doesn't imply any properties or signatures
~
!!! error TS2322: Type '{ [key: string]: number; }' is not assignable to type 'T'.
!!! error TS2322: '{ [key: string]: number; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{ [key: string]: number; }'.
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
~
!!! error TS2339: Property 'x' does not exist on type 'T'.
c[k] = 1; // Error, cannot write to index signature through constraint
~~~~
!!! error TS2322: Type 'number' is not assignable to type 'T[keyof T]'.
!!! error TS2322: 'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'.
}
function f3<K extends string>(a: { [P in K]: number }, b: { [key: string]: number }, k: K) {
a = b; // Error, index signature doesn't imply properties are present
~
!!! error TS2322: Type '{ [key: string]: number; }' is not assignable to type '{ [P in K]: number; }'.
b = a;
a[k];
a[k] = 1;
}
function f3b<K extends string>(a: { [P in K]: number }, b: { [P in string]: number }, k: K) {
a = b; // Error, index signature doesn't imply properties are present
~
!!! error TS2322: Type '{ [x: string]: number; }' is not assignable to type '{ [P in K]: number; }'.
b = a;
}
function f4<K extends string>(a: { [key: string]: number }[K], b: number) {
a = b;
b = a;
}
type Item = { a: string, b: number };
function f10<T extends Item, K extends keyof T>(obj: T, k1: string, k2: keyof Item, k3: keyof T, k4: K) {
obj[k1] = 123; // Error
~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Item'.
!!! error TS7053: No index signature with a parameter of type 'string' was found on type 'Item'.
obj[k2] = 123; // Error
~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'never'.
obj[k3] = 123; // Error
~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'T[keyof T]'.
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'number'.
obj[k4] = 123; // Error
~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'T[K]'.
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'number'.
}
type Dict = Record<string, number>;
function f11<K extends keyof Dict>(obj: Dict, k1: keyof Dict, k2: K) {
obj.foo = 123;
obj[k1] = 123;
obj[k2] = 123;
}
function f12<T extends Readonly<Dict>, K extends keyof T>(obj: T, k1: keyof Dict, k2: keyof T, k3: K) {
obj.foo = 123; // Error
~~~
!!! error TS2339: Property 'foo' does not exist on type 'T'.
obj[k1] = 123; // Error
~~~~~~~
!!! error TS2536: Type 'string' cannot be used to index type 'T'.
obj[k2] = 123; // Error
~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'T[keyof T]'.
!!! error TS2322: 'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'.
obj[k3] = 123; // Error
~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'T[K]'.
!!! error TS2322: 'number' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'number'.
}
// Repro from #27895
export interface Entity {
id: number | string;
}
export type IdOf<E extends Entity> = E['id'];
export interface EntityState<E extends Entity> {
ids: IdOf<E>[];
entities: { [key: string]: E, [key: number]: E };
}
export function getAllEntities<E extends Entity>(state: EntityState<E>): E[] {
const { ids, entities } = state;
return ids.map(id => entities[id]);
}
export function getEntity<E extends Entity>(id: IdOf<E>, state: EntityState<E>): 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<K extends keyof Type>(): Type[K] {
return 123; // Error
~~~~~~~~~~~
!!! error TS2322: Type '123' is not assignable to type 'Type[K]'.
!!! error TS2322: Type 'number' is not assignable to type 'never'.
}
// Repro from #30920
type StrictExtract<T, U> = T extends U ? U extends T ? T : never : never;
type StrictExclude<T, U> = T extends StrictExtract<T, U> ? never : T;
type A<T> = { [Q in { [P in keyof T]: P; }[keyof T]]: T[Q]; };
type B<T, V> = A<{ [Q in keyof T]: StrictExclude<B<T[Q], V>, {}>; }>;
// Repros from #30938
function fn<T extends {elements: Array<string>} | {elements: Array<number>}>(param: T, cb: (element: T['elements'][number]) => void) {
cb(param.elements[0]);
}
function fn2<T extends Array<string>>(param: T, cb: (element: T[number]) => void) {
cb(param[0]);
}
// Repro from #31149
function fn3<T extends ReadonlyArray<string>>(param: T, cb: (element: T[number]) => void) {
cb(param[0]);
}
function fn4<K extends number>() {
let x: Array<string>[K] = 'abc';
let y: ReadonlyArray<string>[K] = 'abc';
}
// Repro from #31439 and #31691
export class c {
[x: string]: string;
constructor() {
this.a = "b";
this["a"] = "b";
}
}
// Repro from #31385
type Foo<T> = { [key: string]: { [K in keyof T]: K }[keyof T] };
type Bar<T> = { [key: string]: { [K in keyof T]: [K] }[keyof T] };
type Baz<T, Q extends Foo<T>> = { [K in keyof Q]: T[Q[K]] };
type Qux<T, Q extends Bar<T>> = { [K in keyof Q]: T[Q[K]["0"]] };
// Repro from #32038
const actions = ['resizeTo', 'resizeBy'] as const;
for (const action of actions) {
window[action] = (x, y) => {
window[action](x, y);
}
}
|