-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
keyof object is number instead of string #41669
Comments
Duplicate of #39543. Used search terms: |
Thanks for the link to #39543 @MartinJohns, but I still think this is a bug. I'm saying it's a bug because I'm explicitly saying that But I think explicit types should be respected. Not to mention there's also an inconsistency between: type GenericObject = { [key: string]: any };
type GenericKeyType = keyof GenericObject; // number | string and type GenericObject = { [key in string]: any };
type GenericKeyType = keyof GenericObject; // string
And I don't think there's any difference between: |
@eek But it's not a bug, it's a design decision. One you probably disagree with, but nonetheless not a bug.
Different syntaxes are for different features. Consistency is not a desirable goal for something like this. |
This is the intended behavior in all cases. |
TypeScript Version: 4.1.2 (actually all versions 3.3.3 - 4.1.2)
Search Terms:
keyof declared object is not only string, keyof object type, keyof declared key, keyof
Code
Expected behavior:
GenericKeyType
should be of typestring
Actual behavior:
GenericKeyType
isnumber | string
Playground Link:
https://www.typescriptlang.org/play?#code/C4TwDgpgBA4hB2EBOBLAxgeQEYCsJuCgF4oBvKAbQGsIQAuKAZ2FXgHMBdBgQ3hCgC+AbgBQI0JFgJk6ANK0AKuGgkaIAPYAzKYlSZc+YEKA
Related Issues:
Didn't find anything.
The text was updated successfully, but these errors were encountered: