Typescript: How to add an item to a tuple?

前端 未结 3 1706
长情又很酷
长情又很酷 2020-12-21 05:23

Why this produces an error \"A rest element type must be an array type.\"?

type QWE = [boolean, ...T]
                              


        
3条回答
  •  情话喂你
    2020-12-21 05:52

    With TypeScript 4.0 variadic tuple types, adding an item I to a tuple type T gets much easier:

    type Prepend = [I, ...T]
    type Append = [...T, I]
    type AddBetween = [...T, I, ...U]
    
    type PrependTest = Prepend 
    // [boolean, string, number]
    
    type AppendTest = Append 
    // [string, number, boolean]
    
    type AddBetweenTest = AddBetween 
    // [string, boolean, number]
    

    Playground sample code

提交回复
热议问题