Here's what I want to accomplish:
const names = ["foo", "bar", "baz"];
type NameType = elementof names; // This is not valid TypeScript
The behavior would be identical to this:
type NameType = "foo" | "bar" | "baz";
A solution will need to accomplish a few things.
- Provide an ordered enumerable sequence of strings.
- Expose the names as an addressable union of string literal types.
- Allow string maintenance without redundant source code edits.
Can TypeScript do this?
Note: This is not the same as Convert string[] literal to string type literal, because that question doesn't require a specifically ordered sequence of strings. Union types don't produce any way to get order information at run time.