Skip to content

Wrong error message (typeof with Array.isArray) #36839

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
MattMoony opened this issue Feb 17, 2020 · 2 comments
Closed

Wrong error message (typeof with Array.isArray) #36839

MattMoony opened this issue Feb 17, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@MattMoony
Copy link

Even though I am well aware that this bug is not one of the most pressing issues at hand and that it is actually not a real problem at all, since all arrays will already produce a truthy value in the first part of the if-clause, I still think that it could lead to some confusion at first glance. I tried my best finding a post mentioning this very specific bug, but I couldn't.

TypeScript Version: 3.7.5

Search Terms: Array.isArray undefined

Code

function foo(bar: number|number[]) {
  if (typeof bar !== 'undefined' || (Array.isArray(bar) && bar.length > 0)) {
    return console.log(':)');
  }
  console.log(':(');
}

foo([1, 2, 3]);

Expected behavior:
Error: Property does not exist on type 'never'

Since Array.isArray(...) will, to the best of my knowledge, never produce a truthy value when given something of type undefined. However, it should produce an Error: Property does not exist on type 'never' error, since the second part of the clause will never be evaluated with something of type array (because that would already be truthy in the first half).

Actual behavior:
Error: Object is possibly 'undefined'

ts-bug

Playground Link:

Related Issues:

@RyanCavanaugh
Copy link
Member

Duplicate #26914

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 19, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants