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

A record value should be narrowed down to array when using Array.isArray #52014

Closed
ficristo opened this issue Dec 24, 2022 · 2 comments
Closed

Comments

@ficristo
Copy link

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

TypeScript 4.9.4

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function foo(id: string) {
  const record: Record<string, number | Array<number>> = {};

  const arr = record[id];

  if (Array.isArray(arr)) {
    // work
    arr.push(1);
  }
  
  if (Array.isArray(record[id])) {
    // doesn't work
    record[id].push(1);
  }
}

πŸ™ Actual behavior

If the record value is used directly is not narrowed down to an array.

πŸ™‚ Expected behavior

The record value type should be narrowed.

@MartinJohns
Copy link
Contributor

Duplicate of #10530. Type narrowing does not occur for indexed access forms e[k] where k is not a literal.

@ficristo
Copy link
Author

In fact using record.id does work, I didn't notice it before. Closing since it is a duplicate.
Thanks.

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