How do I declare a read-only array tuple in TypeScript?

前端 未结 6 1630
太阳男子
太阳男子 2020-12-17 15:07

We can declare a typed tuple in TypeScript, for example, with the type annotation [string, number]. This means an array of 2 elements where the first element ne

6条回答
  •  情深已故
    2020-12-17 15:46

    As of v3.2.2, there's no perfect way of making a readonly tuple type without converting it to an object that looks like an array, but is not.

    The lead architect of TypeScript has said this on the topic of combining Readonly with tuple types.

    Here is the best solution I've come up with:

    type ReadonlyTuple = {
        readonly [P in Exclude]: T[P]
    } & Iterable
    

提交回复
热议问题