gpt4 book ai didi

tsc - 如何用 typescript 表示嵌套数组

转载 作者:行者123 更新时间:2023-12-03 21:21:34 30 4
gpt4 key购买 nike

假设我有一个字符串数组,例如:

const a = ['foo', ['aa'], [['zzz',['bar']]]];

export const acceptsArray = (v: Array<any>) : string => {
returns flattenDeep(v).join(' ');
};

除了使用 Array<any>如何表示嵌套的字符串数组?

最佳答案

简答

新功能 仅适用于 Typescript 3.7+

type A = 'foo' | 'aa' | 'zzz' | 'bar' | A[]

const a:A = ['foo', ['aa'], [['zzz',['bar']]]];

export const acceptsArray = (v: Array<A>) : string => {
returns flattenDeep(v).join(' ');
};

完整答案

需要注意的是,原始作者代码的意图存在歧义。上面的简短回答是一种输入方式,下面是另一种,两者都是有效的输入方式。只有作者才能决定他的真正意图。波纹管打字更严格。

打字的优点是记录程序员的意图。但是由于作者问题没有类型,我无法确切地推断出他的意思。

// When autor say this:
const a = ['foo', ['aa'], [['zzz',['bar']]]];

// Should he mean this:
type A0 = 'foo' | 'aa' | 'zzz' | 'bar' | A[]

// or this:
type A1 = string | string[] | (string | string[])[][]

// Both types 'A0' and 'A1' are valid types for variable 'a',
// but type 'A1' is strictier.

原答案

NOTE: I write bellow answer when TS 3.5 was last version. The important update necessary is mentioned up-above. Bellow answer still valid. I'll refactory the answer to make it shorter when possible.



一种更严格的方法来解决您的问题:

type A = string | string[] | (string | string[])[][]

const a:A = ['foo', ['aa'], [ ['zzz',['bar'] ] ] ];

export const acceptsArray = (v: Array<A>) : string => {
returns flattenDeep(v).join(' ');
};

通用问题

上面的答案有效,但让我们解决您可能更通用的问题:

如何表示任意类型的任意级别的嵌套数组?

嵌套:代表

首先,任何 阵列 有一个 类型 .这是一个字符串数组:

type A = Array<string> // type definition
const a: A = ['a','b','c'] //instance

下面我们更进一步,在一个 Array 中引入另一个 Array。具体来说,我们有一个“字符串数组”数组,仅此而已:

type A = Array<string>
type B = Array<A>
// Ok !
const b0: B = [['a','b','c'],['foobar'],['1','2','3','4','5']] //ok
const b1: B = [['1','2','3']] //ok

//Attention !!!
const b2: B = ['1','2','3'] //error! Array<string> is not equal to Array<Array<string>>
const b3: B = [[1,2,3]] // error! Array<Array<number>> is not equal to Array<Array<string>>

类型 B上面可以写成这样: type B = Array<Array<string>> .在 typescript 中,如果你想要一个“字符串数组数组”,你也可以这样表示:

最短表示

type X0 = Array<Array<string>>
type X1 = string[][] // short form!

同样,如果你想要一个“字符串数组的数组”,你可以这样做:

type Y0 = Array<Array<Array<string>>>
type Y1 = string[][][] // short form!

// this also works!!
type Y2 = (string | number)[][][]

等等...

嵌套:类型检查

重要的是要理解 Typescript 对于您指定为数组类型的内容是严格的。例如:

// Given this types...
type A = string
type B = Array<string>
type Both = A | B

// and this instances...
const a:A = 'foo'
const b:B = ['foo']

// so...

const r0: Array<A> = [a,a] //ok
const r1: Array<B> = [a,a] //error: string is not of type Array<string>
const r2: Array<B> = [b,b] //ok
const r3: Array<Both> = [a,b] //ok



嵌套:通用性

如果我们想要一个类型为 T 的数组怎么办? ,其中 T任何 具体类型。

// Some specific types
type SPECIFIC_TYPE_01 = string
type SPECIFIC_TYPE_02 = number
type SPECIFIC_TYPE_03 = //...etc

// A generic array of any specific type
type GenericArray<T> = Array<T>

// use:
const a: GenericArray<SPECIFIC_TYPE_01> = ['a','b','c'] //ok
const b: GenericArray<SPECIFIC_TYPE_02> = [1,2,3] //ok


Note: Obviously instead of using these long names you may use just Array<string>, Array<number>, etc. I'm just beeing didactic to show how generics works with arrays.



总结

波纹管我使用以上知识来解决您的原始问题。

// Given...
type NestedArray0<T> = T[] //same as Array<T>
type NestedArray1<T> = T[][] //same as Array<Array<T>>
type NestedArray2<T> = T[][][] // same as Array<Array<Array<T>>>
type NestedArray3<T> = T[][][][] // etc you get the point...

// So you can do things like...
const a0: NestedArray0<number> = [1,2,3] //ok
const a1: NestedArray1<number> = [[1,2,3]] //ok
const a2: NestedArray2<number> = [[[1,2,3]]] //ok
const a3: NestedArray3<number> = [[[[1,2,3]]]] //ok

// The type of your examples (based in what you informed) is...
type MyType =
| string // 'foo'
| NestedArray0<string> // ['aa']
| NestedArray1<string | string[]> // [['zzz',['bar']]]

const a: Array<MyType> = ['foo', ['aa'], [['zzz',['bar']]] ]; //ok

// so you can do

export const acceptsArray = (v: Array<MyType>) : string => {
returns flattenDeep(v).join(' ');
};


谢谢你。

关于tsc - 如何用 typescript 表示嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53423733/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com