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

Narrowed "unknown" type not detected inside getter #43676

Closed
samisayegh opened this issue Apr 14, 2021 · 1 comment
Closed

Narrowed "unknown" type not detected inside getter #43676

samisayegh opened this issue Apr 14, 2021 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@samisayegh
Copy link

Bug Report

🔎 Search Terms

narrow, narrowing, getter, unknown

🕗 Version & Regression Information

I noticed this bug on v4.1.2. I can also reproduce it on older versions such as v2.8.1

Please keep and fill in the line that best applies:

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about ___ X ____
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

💻 Code

function wrap(val: unknown) {
  if (typeof val !== 'string') {
    throw new Error();
  }

  console.log(val) // "val" is narrowed to "string" as expected.
  
  return {
    getValue() {
      return val; // "val" is narrowed to "string" as expected.
    },

    get value() {
      return val; // "val" is still "unknown".
    }
  }
}

🙁 Actual behavior

The type is not correctly narrowed inside getters.

🙂 Expected behavior

The type to be narrowed inside getters, as is the case for references in other parts of the function.

@a-tarasyuk
Copy link
Contributor

It seems this issue was resolved in #45006, Playground. So I think it can be closed.

get value() {
>value : string
    return val;
>val : string
}

/cc @andrewbranch

@andrewbranch andrewbranch added the Fixed A PR has been merged for this issue label Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

No branches or pull requests

4 participants