admin管理员组文章数量:1410674
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)[]
.
本文标签: typescriptTuple with a rest element which is a union typeStack Overflow
版权声明:本文标题:typescript - Tuple with a rest element which is a union type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744950677a2634063.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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