Skip to content

interface extends Array need initial index key #35726

@aaabinbin

Description

@aaabinbin

I want to achieve a fixed size array

type FixedLengthArray<T extends any, L extends number> = Array<T> & {
  0: T,
  length: L;
}

type ArrayValue = 'a' | 'b'

type Foo = FixedLengthArray<ArrayValue, 2>;
const foo1: Foo = ['a', 'b'];

But when I remove 0: T, it throws a error, and Array has defined [n: number]: T, what does '0' means.

Type '("a" | "b")[]' is not assignable to type 'FixedLengthArray<ArrayValue, 2>'.
  Type '("a" | "b")[]' is not assignable to type '{ length: 2; }'.
    Types of property 'length' are incompatible.
      Type 'number' is not assignable to type '2'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions