-
Notifications
You must be signed in to change notification settings - Fork 12.4k
-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Index constraint does not constrain #3150
Comments
We changed the behavior here because (at runtime) numbers are implicitly converted to strings during index operations. Basically, a string indexer is a "superset" of a number indexer. |
@RyanCavanaugh ok, thanks! That's slightly annoying though... from what I see that change makes defining the type on an index useless. In my opinion, the runtime behaviour should not be taken into consideration when someone explicitly defines the index type. If someone wanted the current behaviour, I think it would make more sense for them to write:
Oh well :( |
@RyanCavanaugh could you explain why second assignment is incorrect? interface SomeArray1 {
[index: number]: string;
}
var my1: SomeArray1;
my1["12"] = 12;
my1[12] = 12; // <-- why this line is incorrect but first assignment before is OK? I think it's a violation of POLA principle |
We don't treat numeric-looking string literals as numbers, for obvious reasons, so this is the same as writing This is implemented via the global type |
I'm wondering why the following code compiles. class Customer {
dummyProperty: string;
}
var map: { [key: number]: Customer; } = {};
map[14] = new Customer();
map['map'] = new Customer(); // <-- Why does this line compile? Update: It seems it's a problem of the playground. My local tsc raises |
@zjiekai that's because the playground doesn't compile with the |
FYI please don't drop questions into old issues - ask them on Stack Overflow. If you think you've found a bug, log a new issue. |
It seems the index constraint doesn't seem to constrain anymore... if it does, then I blame it being early in the morning.
Copying the code from Ryan's post here:
Playground
Apologies if this has already been raised, but I couldn't find a duplicate issue. Maybe this is now by design?
The text was updated successfully, but these errors were encountered: