microsoft / microsoft/TypeScript

Nullish coalescing operator but for conditional types

Open
#58,629 2 comments 1 reaction 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

nullish coalescing operator
conditional types

✅ Viability Checklist
⭐ Suggestion

Add syntactic sugar to allow this:

type T = A extends B ?? C

to be shorthand of:

type T = A extends B ? A : C
📃 Motivating Example

When working with large complicated types, it is often that you need to check that a type extends another type before passing it as a type argument to another type.

For example, ensuring it is an array before passing it to something that requires it to be an array:

type Foo<T> = Bar<
    Some<Really<Deep<Complex<Type<Utility<T>>>>>> extends readonly unknown[]
        ? Some<Really<Deep<Complex<Type<Utility<T>>>>>>
        : never
>;

type Bar<T extends readonly unknown[]> = ...;

The more complex the type you need to check, the more you need to repeat yourself to pass the same type in again.

This proposal aims to address that.

type Foo<T> = Bar<
    Some<Really<Deep<Complex<Type<Utility<T>>>>>> extends readonly unknown[] ?? never
>;
💻 Use Cases
  1. What do you want to use this for?

Making my type code more readable and easier to maintain.

  1. What shortcomings exist with current approaches?

The need to repeat yourself. Updates to the type need to be make in two places.

  1. What workarounds are you using in the meantime?

The long-form syntax.

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

The issue names no implementation files or tests. Start by tracing how conditional types are parsed and checked, then determine the syntax and type-system changes needed for A extends B ?? C to behave like A extends B ? A : C; done means the shorthand is accepted and has the stated equivalent type behavior.

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
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.