Skip to content

unknown is narrowed to never inside reachable conditional blocks #41833

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
rsimoes opened this issue Dec 5, 2020 · 1 comment
Closed

unknown is narrowed to never inside reachable conditional blocks #41833

rsimoes opened this issue Dec 5, 2020 · 1 comment

Comments

@rsimoes
Copy link

rsimoes commented Dec 5, 2020

TypeScript Version: 4.1.2 & 4.2.0-dev.20201205

Search Terms: unknown never NOT generic

Code

const logIfFoo = (maybeFoo: unknown) => {
  if (maybeFoo instanceof Object && "bar" in maybeFoo) {
    console.log(maybeFoo);
  }
}

logIfFoo({ bar: "baz" });

Expected behavior:
Transpiled JS that yields no output--given the inferred typing of maybeFoo to never inside the conditional block

Actual behavior:
Output of { bar: 'baz' }

Playground Link: https://www.typescriptlang.org/play?ts=4.1.2#code/MYewdgzgLgBANiA5gSQGYDEQhgXhgCgFsBDATwCMBTTEALhgFcwBrMEAdzAEpcA+GAN4BYAFAwYAS1QESFalkmQoxMMEohpAeXIArSsFgAyQzABE5YgCdTimLKo0ewseJihIIOJQB0CRETIHLC4AblFxAF9RCLCRUXdoGFQFPAEYC0t6c2IALxsY0T80GnxkkFDRIA

@MartinJohns
Copy link
Contributor

Duplicate of #38608.

@rsimoes rsimoes closed this as completed Dec 5, 2020
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