[英]Recursive array type typescript
假设我有类型type Atom = string | boolean | number
type Atom = string | boolean | number
type Atom = string | boolean | number
。 我想定义一种类型的数组,如:
NestedArray = Atom | [a_0, a_1, ... , a_n]
NestedArray = Atom | [a_0, a_1, ... , a_n]
其中每个a_i
是一个Atom
或一个NestedArray
。
这可以在打字稿中实现吗?
类型别名不能引用自己,所以这种幼稚的方法会失败:
type NestedArray = Atom | Array<NestedArray | Atom> //Type alias 'NestedArray' circularly references itself.
但是接口可以引用自己:
interface NestedArray extends Array<NestedArray | Atom> {
}
我们可以在顶层定义一个额外的联合来处理根情况:
type Atom = string | boolean | number
interface NestedArray extends Array<NestedArray | Atom> {
}
type AtomOrArray = Atom | NestedArray;
//Usage
let foo: AtomOrArray = [
"",
1,
[1, 2, ""]
]
let bar: AtomOrArray = ""
现在打字稿允许类型循环引用自身,例如:
type RArray = (string | RArray)[];
如果你想在递归函数中定义泛型类型,可以使用interface ...<T> extends (T|...<T>)[]
interface RA<T> extends Array<T | RA<T>> { }
例如,创建一个函数来递归求和
function sum(arr: RA<number>) {
let res = 0;
arr.forEach((n) => {
if (Array.isArray(n)) {
res += sum(n);
} else {
res += n;
}
});
return res;
}
console.log(sum([1, 2, 3, [4, [5]], [[6]]]))
// output: 21
但是这种方式有一些缺点,编译器无法知道n
的具体类型
arr.forEach((n) => {
if (Array.isArray(n)) { // typeof n: `number | RA<number>`
res += sum(n);
} else {
res += n;
}
});
基于这个答案
从 typescript 3.7 开始,您可以创建一个通用的递归类型:
type Tree<T> = T | Array<Tree<T>>
然后像这样使用它:
let stuff: Tree<string | boolean | number> = ["lala", [1, "stuffy"], 3, false];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.