You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm saying it's a bug because I'm explicitly saying that GenericObject must have only string keys, and I've seen the reply saying it's by design decisions of keyof to be able to allow string | number | symbol from here.
But I think explicit types should be respected. Not to mention there's also an inconsistency between:
typeGenericObject={[key: string]: any};typeGenericKeyType=keyofGenericObject;// number | string
And I don't think there's any difference between: [key: string] and [key in string] // sure, the in is meant for other usages but should still be the same.
Activity
MartinJohns commentedon Nov 24, 2020
Duplicate of #39543. Used search terms:
keyof index numberExE-Boss commentedon Nov 24, 2020
Arguably, now that we have #40336 and #40598,
keyofshould report`${number}`for numeric index signatures.eek commentedon Nov 24, 2020
Thanks for the link to #39543 Martin Johns (@MartinJohns), but I still think this is a bug.
I'm saying it's a bug because I'm explicitly saying that
GenericObjectmust have onlystringkeys, and I've seen the reply saying it's by design decisions ofkeyofto be able to allowstring | number | symbolfrom here.But I think explicit types should be respected. Not to mention there's also an inconsistency between:
and
Playground link here
And I don't think there's any difference between:
[key: string]and[key in string]// sure, theinis meant for other usages but should still be the same.MartinJohns commentedon Nov 24, 2020
Radu-Sebastian Amarie (@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.
eek commentedon Nov 24, 2020
But both the variants report the exact same type for
GenericObject:How can then one
keyofreport onlystringand one reportstring | number?RyanCavanaugh commentedon Dec 1, 2020
This is the intended behavior in all cases.