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
It looks like there will be a clean solution for this requirement coming with TypeScript 3.4 version:
With so-called const contexts, the compiler can be told to treat an array or an object as immutable, meaning that their properties are read-only. This also allows the creation of literal tuple types with narrower type inference (i.e. your ["a", "b"] can for the first time be of type ["a", "b"], not string[] without specifiying the whole thing as a contextual type)
The syntax will look like this:
let foo = ["text", 1] as const
or
let foo = ["text", 1]
Here is the extended information of the corresponding PR. As of now, the feature should be available in typescript@next.