-
Notifications
You must be signed in to change notification settings - Fork 33
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
Type of key
in for key in object
loops
#1562
Comments
While the fact that |
We could compile to |
Oh, right, yeah |
This is a famous issue in the TypeScript world; see this FAQ entry and microsoft/TypeScript#12314 The argument is that, just because Civet recently gained the ability to type for key: keyof typeof o, value in o
console.log key, value Unfortunately, it currently compiles to for (const key1 in o) {
const key: keyof typeof o = key1;
const value = o[key1];
console.log(key, value);
} Notably, I don't think we want to change the behavior of And maybe we should always cast when setting |
key
in for key in object
loops
Hmm.. we probably shouldn't do that compilation. After seeing this TS issue I recall that's why we didn't in the first place. It would, in general, be unsound, even for TS's standards. |
@edemaine
Regarding keys, my first example It's the value in the second example I was wishing to have the type I was expecting.
Hmmm. How does that result in I just tried this and this does work
I wonder about the difference between |
Honestly that's pretty weird that To fix this, I think what you really want are "exact" object types. TypeScript object types just specify keys that must exist, but any key might exist. Flow exact object types can specify the exact set of keys that exist. This would enable finer typing of keys and values. We've discussed exact objects for pattern matching, but adding it to the type system is probably beyond the scope of Civet; it needs to be fixed at the TypeScript level. Unfortunately, that probably means it's a long ways off, if it will ever happen. Long-open issue here: microsoft/TypeScript#12936 One more thing I learned reading these issues is that inferred types do behave well with let oActual = {
hello: 24,
world: 42,
}
const out = f(oActual)
//^? const out: "hello" | "world" | "no keys"
function f<T extends object>(o: T extends Record<infer A, infer B> ? T : never) {
for (const key in o) {
return key
}
return "no keys"
} This is pretty interesting. I'm not sure exactly in what settings it works. Maybe there's some magic that Civet could do to make this automatic... but it's definitely not easy. Meanwhile, I think you'll need to explicitly type the key, once #1564 gets released. |
Oh woow!
Yes, of course, makes sense Thank you guys for looking into this, really appreciate it 🙏 |
Closing this as it's more of a TS issue and #1564 provides a viable workaround. |
While doing iterations on objects, the VSC extension gives an incorrect type for the
value
Screen.Recording.2024-11-02.at.16.06.01.mov
Extension version 0.3.25
The text was updated successfully, but these errors were encountered: