-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
For var in object: Var is implicitly any without error #4518
Comments
#3500 is partially related. |
It is! From that thread, it sounds like keys can be |
for the record, this will be a breaking change. |
This might be beside the point but shouldn't the correct code in this case be :
with 'of' instead of 'in'? |
No, because In the case of |
My understanding of ES6 is that for/in iteration should only ever yield For/in iteration is defined in terms of the [[Enumerate]] internal method, which in turn is defined (for non-Proxy objects) in 9.1.11:
As far as I can tell, the only way to get for/in to return non-strings is to be intentionally evil by creating a As an experiment I tried changing tsc to make for/in variables |
Whatever the outcome of this, please make sure that the explicit annotation is allowed in those loops and the catch statement for consistency! Even if it stays with 'any'... |
For-in can only enumerate strings. The current spec allows proxy enumerate to return non-string keys, but this is changed per consensus though not yet in the spec (see: tc39/ecma262#160). |
Sounds very good. Is the explicit annotation as string allowed now? |
@JirkaDellOro Explicit type annotations still aren't supported. The type of a variable declared in for-in is always string, and the type of a variable declared in for-of is always the element type. Allowing annotations would leave the impression that other types are possible which isn't really the case (the only types you could write would be ones that are less specific, which isn't really useful). |
@ahejlsberg Since I'm using TypeScript to teach programming to beginners, having this inconsistency in the language is very irritating. We use explicit type annotations to get them thinking about data types, and since they need the competence when switching to other languages. All is well, just in those loops and in the catch statement, we have exceptions which are not explainable. It doesn't make sense to strictly disallow the annotation of the type that's used implicitly anyway. I'd very much appreciate if we could annotate the type explicitly for the sake of consistency of the syntax, and it would be great to only have string allowed in the for..in loop. Just like it's already implemented for associative arrays: interface ITest { TypeScript tells me: "An index signature parameter type must be 'string' or 'number'." PERFECT! I got the syntax right and consistent, but there is a logical mistake that I can easily fix. We have a limitation of possible types. There already was a lenghty and detailled discussion on this topic here: #3500 PS: TypeScript is great! |
In the following statement,
The variable
subscription
is considered typeany
. Although I'm compiling withnoImplicitAny
as true, no error is given. Based on the for spec, it seems as ifsubscription
should implicitly be consideredstring
.Because of this, it's possible to compile with typo functions that will cause errors at runtime.
The text was updated successfully, but these errors were encountered: