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

array.every not working consistently #52107

Closed
damianobarbati opened this issue Jan 4, 2023 · 4 comments
Closed

array.every not working consistently #52107

damianobarbati opened this issue Jan 4, 2023 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@damianobarbati
Copy link

Bug Report

array.every on union string[][] | string[][][]; throws a This expression is not callable..

πŸ”Ž Search Terms

  • array.every not callable
  • .every not callable
  • every not callable

πŸ•— Version & Regression Information

TS 4.9

⏯ Playground Link

Repro here on TS Playground

πŸ’» Code

type Polygon = string[][] | string[][][];

const p1: Polygon = [['1','1'], ['2','2'], ['3','1'], ['1','1']];
const p2: Polygon = [[['1','1'], ['2','2'], ['3','1'], ['1','1']], [['2','2'],['2.4','2.4'],['3.1']]];

p1.every(console.log); // it works. No complains.
p2.every(console.log); // it works. No complains.

const fn = (polygon: Polygon): void => {
  // .every complains with "this expression is not callable"
  if (polygon.every((p) => p.length === 2)) {
    //
  }
};

πŸ™ Actual behavior

TS throws an error.

πŸ™‚ Expected behavior

TS should not throw an error.

CC: @emilioSp

@MartinJohns
Copy link
Contributor

It's working consistently. Your two variables are not actually typed Polygon because the assignment narrows the type. When you add an as Polygon behind your assignment it won't narrow and the behavior is the same.

@emilioSp
Copy link

emilioSp commented Jan 5, 2023

Ok, I see, but why .every is not callable on union type?

@MartinJohns
Copy link
Contributor

See #36390 and referenced issues. Or search for this expression is not callable union.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 5, 2023
@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

5 participants