[TypeScript] 03 描述数组

47 阅读1分钟

1. String[] | Array<?>


     type A = string[]
     const a: A = ['h', 'i']
    
     等价于 
    
     type A = Array<string>
     

     type B = number[]
     const b: B = [1, 2]
    
     等价于 
    
     type B = Array<number>
     

2.[string]


    数组元素为单一类型  且数量确定

    type D = [string, string, string]
    const noError: D = ['wo', 'ai', 'ni'] //正确
    const error: D = ['h', 'i'] //报错


    type D = [string, string] //二元组
    type D = [string, string, string] //三元组

3.[string, number]


    数组元素为多种类型  且数量确定

    type E = [string, number]
    const e: E = ['王花花'100] 


    数组内嵌套数组

    type F = [string[], number[]]
    const f: F = [['你', '吃', '了', '吗'], [1, 2, 3]]