-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed as not planned
Closed as not planned
Copy link
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
🔎 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
💻 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
Finesse, kaya3 and jonlepage
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created