Skip to content

Fix confusing keywords "as const" and introduce "valueof"Β #63557

@kadun13nindi12

Description

@kadun13nindi12

πŸ” 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:

  1. Add a built-in valueof utility type
  2. 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

  1. Introduce valueof<T> as a built-in utility type equivalent to T[keyof T]
  2. 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

  1. Deriving union types from config/lookup objects (HTTP methods, routes, status codes, event names)
  2. Creating enums-without-enums β€” const objects with derived value unions
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions