microsoft / microsoft/TypeScript

Readonly shorthand cast for expression where `as const` is forbidden

Open
#59,726 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔍 Search Terms

readonly, const readonly, readonly cast, readonly cast expression

✅ Viability Checklist
⭐ Suggestion

Adding a readonly modifier keyword to an expression to cast it automatically to its readonly T counterpart using the T type resulting from the expression.

My main fear is that it could be considered an expression-level syntax and consequently rejected. If this syntax is problematic, is there another possible syntax for providing the same behavior? I have considered as readonly auto, but this auto keyword should then be introduced in TS.

📃 Motivating Example

Assigning a mutable object to a const can lead to subtle bugs (or at least, undesired behaviors) if we expect it to not be modified:

const t = [1, 2, 3] as const
const doubles = t.map(x => x * 2)
doubles.push(4) // OK!

The solution could be to write:

const t = [1, 2, 3] as const
const doubles: readonly number[] = t.map(x => x * 2)
// Or: const doubles = t.map(x => x * 2) as readonly number[]
doubles.push(4) // Error!

But it is more verbose than necessary, since the actual T in the readonly T type could be automatically determined based on the expression.

With this proposal, we could just write:

const t = [1, 2, 3] as const
const doubles = readonly t.map(x => x * 2)
doubles.push(4) // Error!

To cast the expression as its readonly equivalent type.

💻 Use Cases

The main goal is to make converting a type to its readonly counterpart easy to write, to make trivial expressing the appropriate type for const-assigned objects to be truly immutable when it is relevant.

The actual solution works, but is too verbose, since it requires to explicitly write the full type of the expression, making it very undesirable to use, and therefore unlikely to be.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files, tests, or entry points are identified. Start by reviewing the proposed readonly expression syntax and its alternatives, then determine whether the feature fits TypeScript's design constraints; done requires an accepted syntax and an implementation plan with corresponding tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.