-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
error TS2677: Type 'keyof E' is not assignable to type 'string' #23724
Comments
This is a breaking change in TS 2.9 (documentation should come soon) introduced by #23592. With #23592 If you are interested in getting only the string portions of the keys, use function isKey<E>(str: string): str is Extract<keyof E, string> {
return true;
} if your function is meant to handle all property names, including symbols, then consider widening the declaration of function isKey<E> (str : string | number | symbol) : str is keyof E {
return true;
} |
Also, as a temporary measure you can use the new |
Thanks for the swift response! |
In TypeScript 2.9 the constraint of `keyof T` is now `string | number | symbol`. This is not compatible with the type of the `event` parameter in `EventEmitter` methods, which is `string | symbol`. The compiler option `--keyofStringsOnly` reverts to TypeScript's old `keyof T` contraint, and has been added to `tsconfig.json` to resolve this breaking change. More info: microsoft/TypeScript#23724
Because of the `enum-util` library `1.2.0` and the TS2677 error, detailed in microsoft/TypeScript#23724
For typescript 4, I use this in place of keyof as a current workaround to get accurate key types type KeyTypes<T> = {
[K in keyof T]-?: K extends string ?
string :
K extends number ?
number :
K extends symbol ?
symbol :
never
}[keyof T];
type KeyOfType<T, KeyType extends string | number | symbol = KeyTypes<T>> = Extract<keyof T, KeyType>; |
TypeScript Version: Version 2.9.0-dev.20180426
Search Terms: keyof not assignable string
Code
Expected behavior:
Should compile fine. The body of the type guard may actually correctly assert that
str
is, indeed,keyof E
Works fine on TypeScript 2.8.3
Actual behavior:
Playground Link: Works on the playground
Related Issues:
The text was updated successfully, but these errors were encountered: