-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Object key type should be forced to be a string (index signature parameter) #44577
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
This is intentional and has been the behavior for... ever? Changing this would be a massive breaking change. |
I'm aware that this has been the way it was forever, hence:
I understand this will be a breaking change, I don't expect this to be changed in a minor version bump, but it's a dangerous design decision since it sets the wrong expectation for the type of the properties, especially in a language where types are supposed to be more controllable. I can see that looping through the properties shows the property as a string and trying to compare using |
I don't understand what's being suggested here. Where specifically do you want an error, or do you just disagree with the existence of numeric index signatures in general? |
I disagree with the ability to set index type to const a: {[prop: number]: number} = {};
a[1] = 123;
for (const i in a) {
console.log(a, a[i], typeof i);
} The output of the {
"1": 123
}, 123, "string" |
I'll change this to a feature request, should I edit the original post, add a new comment or completely start a new issue or remove this and just upvote issue #43041? |
This is the intended behavior. Numeric index signatures mean that when you index an object by a key (which, yes, are always strings) which was produced from the coercion of a number, a particular property type is produced. It doesn't imply that a different storage format is being used at runtime. We are not accepting feature requests to remove language constructs. |
That's great in theory, but due to the way types work in the rest of the language, it isn't immediately obvious. There's also this: It would be nicer to have a more concise way to specify a type as a I was aware of this for a while, but I've seen people make this mistake and it's easy to understand why, it's simply not concise enough. This would also be a great way to allow this: const a: {[prop: number]: number} = {};
a[1] = 123;
a['2'] = 321; // this currently doesn't work |
The type system is largely constructed of such artificial distinctions - at runtime there's no difference between an empty
|
I'm well aware that at runtime the types don't truly matter since you could save whatever type you want into whatever variable. The point remains that Some kind of numeric string type should be created or at least a type that can represent this in a more accurate way. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
So should I make a new feature request to create a new type for this purpose instead of using |
@rosenbergd if you like; please keep backcompat in mind if you want it to be seriously considered |
Just linking this to the new issue created |
Bug Report
π Search Terms
Object key type, index signature parameter type
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
I'm allowed to set the
prop
key to typenumber
, but it is actually astring
when testedπ Expected behavior
It should not allow me to use
number
as the type for the key properties as it automatically converts tostring
anyways which could create confusionThe text was updated successfully, but these errors were encountered: