This works:
type number_ = number | null;
let myTuple: [string, ...number_[]];
myTuple = ['foo', 1, 2, 3];
myTuple = ['foo', 1, null, 3];
const [a, ...b] = myTuple;
// Type checker knows a is string, and b is array of number | null
Is there any way to express that without using number_
?
This works:
type number_ = number | null;
let myTuple: [string, ...number_[]];
myTuple = ['foo', 1, 2, 3];
myTuple = ['foo', 1, null, 3];
const [a, ...b] = myTuple;
// Type checker knows a is string, and b is array of number | null
Is there any way to express that without using number_
?
1 Answer
Reset to default 1You can avoid creating a special type by declaring the union inside parenthesis before the array symbol:
let myTuple: [string, ...(number | null)[]];
Doing it this way will still cause the variable b
from your example to be typed as (number | null)[]
.
number_
"? Sure, like this. But do you really just mean you want to avoid a named type? Could you edit to clarify what you mean here? – jcalz Commented Mar 6 at 20:42