Infer type inside switch/case statement using instanceof
#20957
Labels
Duplicate
An existing issue was already created
instanceof
#20957
I'm not sure if it's a bug or a feature request. I have the following simple example:
I get an error on the logging lines because typescript can't know whether
p
is of typeT1
orT2
. However I useinstanceof
operator so it should be able to infer the correct type as it works fine withif
statement:TS version 2.6.2
I know I can fix it using explicit type casting like this:
console.log((<T1>p).a);
but that seems redundant. Also interesting that type guard doesn't work:
The text was updated successfully, but these errors were encountered: