Typescript: Can I define an n-length tuple type?

后端 未结 3 782
轮回少年
轮回少年 2020-12-06 00:50

I am creating a shogi game board using Typescript. A shogi board has 9 ranks and files.

I\'d like to assert a 9x9 multidimensional array as a type to ensure both the

3条回答
  •  死守一世寂寞
    2020-12-06 01:27

    type PushFront = (
      ((front : FrontT, ...rest : TailT) => any) extends ((...tuple : infer TupleT) => any) ?
      TupleT :
      never
    )
    
    type Tuple = {
      0 : OutputT,
      1 : Tuple>
    }[
      OutputT["length"] extends LengthT ?
      0 :
      1
    ]
    
    //type t3 = [string, string, string]
    type t3 = Tuple
    //type length = 0 | 3 | 1 | 2
    type length = Partial>['length']
    

    Add a generic way to specify length of a tuple type #issuecomment-513116547

提交回复
热议问题