Narrow object property indexed by bracket notation when the key is a known literal type, regardless of where the key comes from #61176
Labels
Help Wanted
You can do this
Possible Improvement
The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
π Search Terms
type guard, narrowing, control flow analysis, property, index, known type, literal type, bracket notation
β Viability Checklist
β Suggestion
This is a different re-opening or re-focusing of #10530, not the same as #56389, and was not fixed by #57847. The original motivating example for #10530 (treat
obj["key"]
likeobj.key
for narrowing) was fixed long ago, and more recently many of the remaining requests which had been added to #10530 were also fixed (narrowing forobj[key]
whenkey
is a variable whose literal type isn't known, for multiple uses of the samekey
). Those issues are closed. But it seems there are still some issues lumped in with #10530 which were not addressed, so here's the suggestion:Please enable narrowing of object properties accessed via bracket notation based on the type of the key whenever the key's type is a known literal, even if multiple distinct variables or properties are used as keys. That is, if you have
key1
andkey2
andkeyObj.prop
and they are all of the literal type"foo"
(or have been narrowed to that type), then treatobj[key1]
andobj[key2]
andobj[keyObj.prop]
all asobj.foo
for control flow purposes.π Motivating Example
#51368 gives one, where the variable is a
let
variable annotated with a literal type orconst
asserted :Probably I'd say someone should use a
const
there instead oflet
.There's also one from this Stack Overflow question, involving
enum
-likeconst
-asserted objects:This one strikes me as quite unfortunate, since an actual
enum
works just fine here:π» Use Cases
See #10530 and various issues closed as duplicates for more use cases.
If TS knows the literal key then you do too, so you could always just use it directly:
That's reasonable, but I think it would be nice if there weren't this caveat for
enum
-like objects where you can't actually use them as keys directly for narrowing purposes.Playground link to code
The text was updated successfully, but these errors were encountered: