Skip to content

Typeguard fails in function after instanceof #10273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hpop opened this issue Aug 11, 2016 · 1 comment
Closed

Typeguard fails in function after instanceof #10273

hpop opened this issue Aug 11, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@hpop
Copy link

hpop commented Aug 11, 2016

TypeScript Version: nightly (2.0.0-dev.201xxxxx)

Code

class A {
  foo = 'hello';
}

class B extends A {
  bar = 'hello';
}

let a:A = new B();

if (a instanceof B) {
  console.log(a.bar); // working
}

if (a instanceof B) {
  setTimeout( () => {
    console.log(a.bar); // error
  });
}

Expected behavior:
TypeScript should recognize a as instance of B. This worked in 1.8

Actual behavior:
test.ts(17,17): error TS2339: Property 'bar' does not exist on type 'A'.

@roganov
Copy link

roganov commented Aug 11, 2016

Duplicate of #7719 (and others). Declaring const a:A = new B(); instead of let a:A = new B(); should help.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 11, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants