Typescript 声明任意嵌套数组(递归类型定义)

Typescript 声明任意嵌套数组(递归类型定义),typescript,tsc,typescript3.0,Typescript,Tsc,Typescript3.0,假设我有这样一个函数: const nested = function(v: string | Array<string> | Array<Array<string>>){...} 您可以很容易地描述任意嵌套的数组类型,如下所示: interface NestedArray<T> extends Array<T | NestedArray<T>> { } 但编译器会在五个级别之后放弃检查此类嵌套类型: // no err

假设我有这样一个函数:

const nested = function(v: string | Array<string> | Array<Array<string>>){...}

您可以很容易地描述任意嵌套的数组类型,如下所示:

interface NestedArray<T> extends Array<T | NestedArray<T>> { }
但编译器会在五个级别之后放弃检查此类嵌套类型:

// no error!  
const what: NestedArray<number> = [[[[["a"]]]]]; // this way work:

    type NestedArray<T> = T | NestedArray<T>[];
    const arr: NestedArray<string> = [[[["zimm"]]]];
//没有错误!
常量:NestedArray=[a];// 这样工作:

类型NestedArray=T | NestedArray[];
const arr:NestedArray=[[[“zimm”]]];
Github上的相关问题:
// works as expected
const nums: NestedArray<number> = [1,[2,[3,[4,[5],6,[7]],[8]],[[9]]]];

// errors as expected
const oops: NestedArray<number> = [1,[2,["3",[4,[5],6,[7]],[8]],[[9]]]]; // error
// no error!  
const what: NestedArray<number> = [[[[["a"]]]]]; // this way work:

    type NestedArray<T> = T | NestedArray<T>[];
    const arr: NestedArray<string> = [[[["zimm"]]]];