Skip to content
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

Class field does not apply control statement correctly when using it inside a function #58465

Closed
zhiyan114 opened this issue May 8, 2024 · 1 comment

Comments

@zhiyan114
Copy link

zhiyan114 commented May 8, 2024

⚙ Compilation target

ESNext

⚙ Library

v5.1.3

Missing / Incorrect Definition

When a class field method contains 2 or more types, using if statement can help confirm and expose the object types. But when that same field is used inside a wrapped function, the check no longer works, exposing the default type as if the control statement doesn't exist at all.

Workaround: Set the field as a separate independent variable and the check will pass through.

Sample Code

class A {
  test = 1;
  testCall() {
    return 1;
  }
}

class B {
  tester = 2;
  testCaller() {
    return 2;
  }
}

class C {
  testee: A | B;
  testCaller() {
    return 33;
  }
}

function test() {
  let D: C = SomeFunctionThatSpawnsC();
  let E = D.testee;
  if(!(D.testee instanceof B)) 
    return;
  if(!(E instanceof B))
    return;
  D.testee.tester; // works
  E.tester; // works
  const F = () => {
    D.testee.tester // Type Error
    E.tester // Works
  }
}

Documentation Link

No response

@MartinJohns
Copy link
Contributor

Another duplicate of #9998.

@zhiyan114 zhiyan114 closed this as not planned Won't fix, can't repro, duplicate, stale May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants