Converting TypeScript Arrays to String Literal Types delves into the creation of a string literal type from an array. The question raised is whether it's feasible to derive a string literal from an existing array.
Using the same example:
const furniture = ['chair', 'table', 'lamp'];
type Furniture = 'chair' | 'table' | 'lamp';
The established approach during declaration is:
const furniture = ['chair', 'table', 'lamp'] as const;
This enforces the array as readonly
. Is there a way to transform the array directly?
const furniture = ['chair', 'table', 'lamp'];
const furntureLiteral = furniture as const;
// Results in: "readonly [string[]]" typing.
// Desired outcome: "readonly ["chair", "table", "lamp"]" typing.
Or does static typing hinder this possibility?