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
This may be a dupe of #11664 though that deals with a type union and this is purely inheritance.
TypeScript Version: 2.3.2
Code
abstractclassValue{readonlyvalueType: number;abstractvalue(): any;}classNullValueextendsValue{valueType=0;value(): any{returnnull;}}classNumberValueextendsValue{valueType=1;constructor(readonlynum: number){super();}value(): any{returnthis.num;}}functionprocess(node: Value){if(nodeinstanceofNullValue){console.log('Null!')}elseif(nodeinstanceofNumberValue){// Produces: TS2339: Property 'num' does not exist on type 'never'.console.log('NumberValue',node.num);}}
Expected behavior: No errors.
Actual behavior: Error:
test.ts(29,37): error TS2339: Property 'num' does not exist on type 'never'.
I can "solve" the issue by adding a dummy readonly foo = null; field to the NullValue class.
The text was updated successfully, but these errors were encountered:
The issue here is that, as you suggest, NullValue is a structural supertype of NumberValue. Since you test for NullValue first, the typechecker concludes that that node cannot be a NumberValue since it did not meet the requirements to be a value of its supertype.
By the same token, the following assignment is legal
constx : NullValue=newNumberValue(1);
However, in the case of the process function above, if one just reverses the order of the checks like so
This may be a dupe of #11664 though that deals with a type union and this is purely inheritance.
TypeScript Version: 2.3.2
Code
Expected behavior: No errors.
Actual behavior: Error:
I can "solve" the issue by adding a dummy
readonly foo = null;
field to the NullValue class.The text was updated successfully, but these errors were encountered: