Allow direct property check if (x.a)
to work and narrow type if at least one type in typeof x
has a
#38156
Closed
5 tasks done
Labels
Working as Intended
The behavior described is the intended behavior; this is not a bug
Search Terms
condition property if in guard
Suggestion
I would like the following to work:
Use Cases
This is very common pattern in JavaScript, so when rewriting some of my JS code to TypeScript I often have to change this.
E.g.
I can rewrite this in two ways.
First
This has two issues:
fromTask
, so if I made a typo (fromTsk
) it would not be found.Second
This works, but feels really artificial -- it does not give any information that previous signature didn't have, and the only purpose is to work around the
if
error.Examples
Union
No such property
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: