特定の個数を持つ配列を作る

2021/4/25
TypeScript

FYI: https://stackoverflow.com/questions/41139763/how-to-declare-a-fixed-length-array-in-typescript

type ArrayLengthMutationKeys = 'splice' | 'push' | 'pop' | 'shift' |  'unshift'
type FixedLengthArray<T, L extends number, TObj = [T, ...Array<T>]> =
  Pick<TObj, Exclude<keyof TObj, ArrayLengthMutationKeys>>
  & {
    readonly length: L 
    [ I : number ] : T
    [Symbol.iterator]: () => IterableIterator<T>   
  }
const myFixedLengthArray: FixedLengthArray<number,3> = [1, 2, 3];