π Search Terms
as const, valueof
β
Viability Checklist
β Suggestion
Title: Feature Request: Introduce valueof utility type and clarify as const semantics
Summary
Two related ergonomics improvements:
- Add a built-in
valueof utility type
- Consider aliasing or renaming
as const to better reflect what it actually does
Problem
1. No valueof utility type
Getting a union of an object's values currently requires a non-obvious incantation:
const HTTP_METHODS = { get: 'GET', post: 'POST' } as const;
type HttpMethod = typeof HTTP_METHODS[keyof typeof HTTP_METHODS];
// 'GET' | 'POST'
This is one of the first things developers need and one of the hardest patterns to discover or remember. A valueof utility type would make this trivial:
type HttpMethod = valueof<typeof HTTP_METHODS>;
// 'GET' | 'POST'
keyof exists for keys β valueof should exist for values. The asymmetry is confusing.
2. as const is a misleading name
as const does two things:
- Makes all properties
readonly
- Narrows types to their literal values (e.g.
string β 'GET')
The name as const suggests it relates to the const keyword, but const variables are already const. What as const actually does is narrow types to literals and make properties readonly. A name like as readonly or as literal would be far more self-documenting.
Suggested Solution
- Introduce
valueof<T> as a built-in utility type equivalent to T[keyof T]
- Consider introducing
as readonly as an alias or replacement for as const
Why This Matters
These two concepts (as const + keyof typeof) are among the most commonly googled TypeScript patterns by developers at all experience levels. The current API has a steep discoverability cliff that a simple addition would eliminate.
π Motivating Example
A developer wants a union of all values in a config object:
const HTTP_METHODS = { get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE' } as const;
// Current β non-obvious, hard to discover:
type HttpMethod = typeof HTTP_METHODS[keyof typeof HTTP_METHODS];
// Proposed β intuitive, mirrors keyof - without need of "as const" before:
type HttpMethod = valueof;
"as const" could remain for the second intended purpose - to make all properties readonly and preferably also renamed to "as readonly"
π» Use Cases
- Deriving union types from config/lookup objects (HTTP methods, routes, status codes, event names)
- Creating enums-without-enums β const objects with derived value unions
- Any time a developer needs "give me a type that can be any value in this object"
This pattern is so common it appears in nearly every TypeScript codebase, yet it requires knowledge of three separate concepts (as const, typeof, keyof) that beginners and intermediate developers consistently struggle to combine correctly.
π Search Terms
as const, valueof
β Viability Checklist
β Suggestion
Title: Feature Request: Introduce
valueofutility type and clarifyas constsemanticsSummary
Two related ergonomics improvements:
valueofutility typeas constto better reflect what it actually doesProblem
1. No
valueofutility typeGetting a union of an object's values currently requires a non-obvious incantation:
This is one of the first things developers need and one of the hardest patterns to discover or remember. A
valueofutility type would make this trivial:keyofexists for keys βvalueofshould exist for values. The asymmetry is confusing.2.
as constis a misleading nameas constdoes two things:readonlystringβ'GET')The name
as constsuggests it relates to theconstkeyword, butconstvariables are already const. Whatas constactually does is narrow types to literals and make properties readonly. A name likeas readonlyoras literalwould be far more self-documenting.Suggested Solution
valueof<T>as a built-in utility type equivalent toT[keyof T]as readonlyas an alias or replacement foras constWhy This Matters
These two concepts (
as const+keyof typeof) are among the most commonly googled TypeScript patterns by developers at all experience levels. The current API has a steep discoverability cliff that a simple addition would eliminate.π Motivating Example
A developer wants a union of all values in a config object:
const HTTP_METHODS = { get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE' } as const;
// Current β non-obvious, hard to discover:
type HttpMethod = typeof HTTP_METHODS[keyof typeof HTTP_METHODS];
// Proposed β intuitive, mirrors keyof - without need of "as const" before:
type HttpMethod = valueof;
"as const" could remain for the second intended purpose - to make all properties readonly and preferably also renamed to "as readonly"
π» Use Cases
This pattern is so common it appears in nearly every TypeScript codebase, yet it requires knowledge of three separate concepts (as const, typeof, keyof) that beginners and intermediate developers consistently struggle to combine correctly.