-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 signature pointing to any
is too relaxed
#24083
Comments
Works as expected. Reason:This errors because
This works because type T = {
[p: string]: any
}
function f(t: T) { }
f([]) More
|
If the reason why your second snippet works really was that " type T = {
[p: string]: undefined
// Or: [p: string]: void
}
function f(t: T) { }
f([]) TypeScript error says "index signature is missing". If an index signature is expected, however it is defined, and a type is provided with no index signature, it should fail. I don't understand how anyone could expect a different behaviour. We already have |
This used to be the behavior and we intentionally changed it; see #4074. There's a very long discussion leading up to that in #3823. See also #3823 (comment) |
It is unclear to me that the change was in the right direction. I don't see what benefit rendering function f(o: { n: number, [key: string]: any }) { }
function g<O extends { n: number }>(o: O) { } With its prior meaning you could at least force a type to simply define an index signature, and restrict the type of its key. (In this sense, I think it would have made sense to allow optional index signatures, with the meaning "if an index signature is present, it must comply with such and such restriction".) There is another point which at the moment I'm not seeing clearly -- how can one define a function which accepts a plain object? |
function f(x: object) { |
f([]) |
Correct, an array is a subtype of |
Yes, but I was asking for a plain object. As in |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Just to come to a conclusion, there is no way in TypeScript to express the idea of a plain object, and you will not make stricter the index signatures again. Is that correct? |
function f(x: object & { push?: never }) {
}
f({}); // OK
f([]); // Error We do not currently intend to change anything around index signatures |
Not sure why you insist on But OK, I won't insist more... I guess I'm still trying to value correctness over convenience, but the majority wants it otherwise. |
How come this code fails...
...and even this one...
...but this one doesn't?
I'm sorry if this has been pointed out before; it's difficult for me to look for related issues.
The text was updated successfully, but these errors were encountered: