Skip to content

instanceof / structural typing issue #15585

@mikelehen

Description

@mikelehen

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

abstract class Value {
  readonly valueType: number;
  abstract value(): any;
}

class NullValue extends Value {
  valueType = 0;

  value(): any {
    return null;
  }
}

class NumberValue extends Value {
  valueType = 1;

  constructor(readonly num: number) { super(); }

  value(): any {
    return this.num;
  }
}

function process(node: Value) {
  if (node instanceof NullValue) {
    console.log('Null!')
  } else if (node instanceof NumberValue) {
    // 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.

Metadata

Metadata

Assignees

Labels

FixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions