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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type narrowing with "in" operator and closure results in an "any" type #53913

Closed
jdufresne opened this issue Apr 19, 2023 · 2 comments
Closed

Comments

@jdufresne
Copy link

jdufresne commented Apr 19, 2023

Bug Report

Example code:

const keys = ["b"];

type A = {
  b?: boolean;
};

function f(a: A): void {
  keys.map((key) => {
    if (key in a) {
      const c: never = a[key];
    }
  });
}

馃檨 Actual behavior

test.ts:10:13 - error TS2322: Type 'any' is not assignable to type 'never'.

10       const c: never = a[key];
               ~

馃檪 Expected behavior

test.ts:10:13 - error TS2322: Type 'boolean' is not assignable to type 'never'.

10       const c: never = a[key];
               ~

We can deduce that because the key ("b") is in the variable "a", a[key] must be a boolean.

@jcalz
Copy link
Contributor

jcalz commented Apr 19, 2023

You didn't fill out the issue template completely; could you edit to do that?

This is behaving as designed and there's no bug; if we interpret this as a feature request then it's a duplicate of #43284 (awaiting more feedback) or #48149 (declined).

@jdufresne
Copy link
Author

Thanks for the response. I'll close in favor of those linked issues.

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