My goal is to achieve the following:
const names = ["foo", "bar", "baz"];
type NameType = elementof names; // This is an invalid TypeScript syntax
The desired outcome should mirror the behavior of this code:
type NameType = "foo" | "bar" | "baz";
To solve this, we need a solution that can:
- Create a list of strings in a specific order.
- Allow easy access to individual string literals.
- Enable updating string values without needing to modify the source code repeatedly.
Is it possible for TypeScript to meet these requirements?
Please note: This question differs from the query about Converting string[] literal to string type literal as it specifically calls for maintaining the order of strings. Union types do not provide runtime order information.