Open
Description
π Search Terms
"arguments length"
π Version & Regression Information
It works the same in v3.3.3
β― Playground Link
π» Code
Some basic ways to check if we have any arguments could be these:
function example1(...args: [number] | []) {
if (args.length > 0) {
return args[0];
} else {
return NaN;
}
}
function example2(...args: [number] | []) {
if (args.length) {
return args[0];
} else {
return NaN;
}
}
function example3(...args: [number] | []) {
if (args.length === 0) {
return NaN;
} else {
return args[0];
}
}
But TS doesn't seem to understand the first one.
π Actual behavior
It doesn't understand the way it's done in "example1".
π Expected behavior
It should understand the way it's done in "example1".
Additional information about the issue
Type inference on "args" after the check for "example1":
Type inference on "args" after the check for "example2":
Type inference on "args" after the check for "example3":
Notice how for "example2" and "example3" the type is correctly narrowed to just [number]
, but for "example1" the unnarowed [number] | []
type remains.