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

Type narrowing for if (!Array.isArray(a)) { fails to eliminate a readonly array #57566

Closed
brad4d opened this issue Feb 27, 2024 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@brad4d
Copy link

brad4d commented Feb 27, 2024

🔎 Search Terms

"Array.isArray", readonly, narrow, "!Array.isArray"

🕗 Version & Regression Information

  • This is the behavior in every version I tried from the latest back to 4.9.5, and I reviewed the FAQ for entries about Array.isArray, narrowing, and readonly arrays.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMAFAQwFyLCAtgIwFMAnRAH0WMLQBMEAbAT2zyOIG0BdASkQG8AUIkQxgiFAEIAgsWJpGAOhgBnGXMbpuvQcOEQEyuPUIL6cAObpEAakRpuAbiGIAvgLcDQkWAkSXMLAQk5IFsXNrOouLSsvJKqrEa9hG6iPpghsamFla29k7Cbm5AA

💻 Code

function f(a: number | readonly number[]) {
  if (!Array.isArray(a)) {
    console.log(a + a); // error here
  }
}

function g(a: number | number[]) {
  if (!Array.isArray(a)) {
    console.log(a + a); // no error here
  }
}

🙁 Actual behavior

TypeScript reports "Operator '+' cannot be applied to types 'number | readonly number[]' and 'number | readonly number[]'."

for the a + a expression inside

function f(a: number | readonly number[]) {
  if (!Array.isArray(a)) {
    console.log(a + a); // error here
  }
}

🙂 Expected behavior

Expect that narrowing variable a of type number | readonly number[] using if (!Array.isArray(a)) { should narrow the type of a to number.
That is what happens when a is of type number | number[].

Additional information about the issue

No response

@brad4d brad4d changed the title Type narrowing for if (!Array.isArray(a) { fails to eliminate a readonly array Type narrowing for if (!Array.isArray(a)) { fails to eliminate a readonly array Feb 27, 2024
@Andarist
Copy link
Contributor

Duplicate of #53395

This function is defined as (method) ArrayConstructor.isArray(arg: any): arg is any[]. A readonly array doesn't satisfy the return type of this predicate (a readonly array has no .push method and more) and thus it's not eliminated from the union.

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

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2024
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

4 participants