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
|
error TS5101: Option 'keyofStringsOnly' is deprecated and will stop functioning in TypeScript 5.5. Specify compilerOption '"ignoreDeprecations": "5.0"' to silence this error.
tests/cases/compiler/keyofDoesntContainSymbols.ts(11,30): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
tests/cases/compiler/keyofDoesntContainSymbols.ts(14,23): error TS2345: Argument of type 'unique symbol' is not assignable to parameter of type '"str" | "num"'.
tests/cases/compiler/keyofDoesntContainSymbols.ts(17,23): error TS2345: Argument of type '0' is not assignable to parameter of type '"str" | "num"'.
!!! error TS5101: Option 'keyofStringsOnly' is deprecated and will stop functioning in TypeScript 5.5. Specify compilerOption '"ignoreDeprecations": "5.0"' to silence this error.
==== tests/cases/compiler/keyofDoesntContainSymbols.ts (3 errors) ====
const sym = Symbol();
const num = 0;
const obj = { num: 0, str: 's', [num]: num as 0, [sym]: sym };
function set <T extends object, K extends keyof T> (obj: T, key: K, value: T[K]): T[K] {
return obj[key] = value;
}
const val = set(obj, 'str', '');
// string
const valB = set(obj, 'num', '');
~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
// Expect type error
// Argument of type '""' is not assignable to parameter of type 'number'.
const valC = set(obj, sym, sym);
~~~
!!! error TS2345: Argument of type 'unique symbol' is not assignable to parameter of type '"str" | "num"'.
// Expect type error
// Argument of type 'unique symbol' is not assignable to parameter of type "str" | "num"
const valD = set(obj, num, num);
~~~
!!! error TS2345: Argument of type '0' is not assignable to parameter of type '"str" | "num"'.
// Expect type error
// Argument of type '0' is not assignable to parameter of type "str" | "num"
type KeyofObj = keyof typeof obj;
// "str" | "num"
type Values<T> = T[keyof T];
type ValuesOfObj = Values<typeof obj>;
|