You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Narrowing the key obtained by iterating through Object.entries() does not narrow the type of the value associated with that key. I.e., inside for (const [key, value] of Object.entries(x)) {…}, x[key] can have its type narrowed by narrowing key, but the type of value will not be narrowed, even though the two are equivalent.
Right now, it feels somehow like a surprise that x[key] is not the same as value and x[key] needs to be evaluated again, even though that was the original goal of using the iterator.
🔍 Search Terms
object type narrowing key value
✅ Viability Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
typeX={a: number,b: number,c: string};functionisKeyToNumber(s: string): s is 'a'|'b'{returns==='a'||s==='b';}exportfunctiondoubleNumbers(x: X): void{for(const[k,v]ofObject.entries(x)){if(isKeyToNumber(k)){consty=v*2;// v is not known to be a number ❌constz=x[k]*2;// x[k] is known to be a number ✅}}}
I ran into this issue while writing a sort function for a TypeScript-first vCard parser, which I mis-attributed first.
💻 Use Cases
Enumerating through objects which have different types for their values based on the key, e.g.
for databases
JSON inputs
vCards
…
Right now,
for…of cannot be reasonably used there, as the value's type is not narrowed, unless re-fetched from the record; and
the developer is confused why the same data has two different types, depending on how they try to get at it.
The text was updated successfully, but these errors were encountered:
I don't see how this could be implemented without a very specific implementation just for Object.entries(), and that would be silly and counterproductive. But I'd love to learn and be corrected.
Suggestion
Narrowing the key obtained by iterating through
Object.entries()
does not narrow the type of the value associated with that key. I.e., insidefor (const [key, value] of Object.entries(x)) {…}
,x[key]
can have its type narrowed by narrowingkey
, but the type ofvalue
will not be narrowed, even though the two are equivalent.Right now, it feels somehow like a surprise that
x[key]
is not the same asvalue
andx[key]
needs to be evaluated again, even though that was the original goal of using the iterator.🔍 Search Terms
object type narrowing key value
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
📃 Motivating Example
Example (Playground):
I ran into this issue while writing a sort function for a TypeScript-first vCard parser, which I mis-attributed first.
💻 Use Cases
Enumerating through objects which have different types for their values based on the key, e.g.
Right now,
for…of
cannot be reasonably used there, as the value's type is not narrowed, unless re-fetched from the record; andThe text was updated successfully, but these errors were encountered: