Type narrowing for if (!Array.isArray(a)) {
fails to eliminate a readonly array
#57566
Labels
Duplicate
An existing issue was already created
🔎 Search Terms
"Array.isArray", readonly, narrow, "!Array.isArray"
🕗 Version & Regression Information
Array.isArray
, narrowing, and readonly arrays.⏯ Playground Link
https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMAFAQwFyLCAtgIwFMAnRAH0WMLQBMEAbAT2zyOIG0BdASkQG8AUIkQxgiFAEIAgsWJpGAOhgBnGXMbpuvQcOEQEyuPUIL6cAObpEAakRpuAbiGIAvgLcDQkWAkSXMLAQk5IFsXNrOouLSsvJKqrEa9hG6iPpghsamFla29k7Cbm5AA
💻 Code
🙁 Actual behavior
TypeScript reports "Operator '+' cannot be applied to types 'number | readonly number[]' and 'number | readonly number[]'."
for the
a + a
expression inside🙂 Expected behavior
Expect that narrowing variable
a
of typenumber | readonly number[]
usingif (!Array.isArray(a)) {
should narrow the type ofa
tonumber
.That is what happens when
a
is of typenumber | number[]
.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: