microsoft / microsoft/TypeScript

InitialOnly Utility Type

Open
#45,610 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Suggestion

I have found myself more and more often using a tri-state boolean approach in my code. By that I mean a boolean that has an inital value of null and will be reset to true or false

Currently we would do something like:

let example: boolean | null = null;

However, I find the use case when going to route is often a set it one time and null then being an invalid value.

An argument may be why not use undefined well I believe the intentional aspect of setting it to null provides context and for more readable code. I also feel the creating of a tri-state enum wouldn't be the correct approach as well.

A utility type stating a value is acceptable only as an initial value and not on reassignment is the request.

🔍 Search Terms

tristate boolean, utility state

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

utility type, tri-state boolean

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Utility type for a value only being acceptable on initialization.

📃 Motivating Example

I would talk about a tri-state boolean example.

💻 Use Cases

Work arounds were mention in main description.

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 source files, tests, or entry points are named. Start by reviewing the TypeScript design goals and the proposed initialization-only utility type, then identify the relevant type-system area. Done would require a precise type-checking behavior and tests demonstrating initialization versus reassignment.

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
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.