作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个字符串数组,例如:
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
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)[][][]
// 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/
我是一名优秀的程序员,十分优秀!