TypeScript array to string literal type asks and receives an answer regarding how to create a string literal upon array declaration. I am wondering if it is possible to create a string literal from an already existing array.
To take the same basic example:
const furniture = ['chair', 'table', 'lamp'];
type Furniture = 'chair' | 'table' | 'lamp';
the suggested known solution at declaration is:
const furniture = ['chair', 'table', 'lamp'] as const;
This locks the array to a readonly
. Is it possible to just take the array and make a new item from it?
const furniture = ['chair', 'table', 'lamp'];
const furntureLiteral = furniture as const;
// Yields: "readonly [string[]]" typing.
// Wanted: "readonly ["chair", "table", "lamp"]" typing.
Or is it impossible due to static typing?