microsoft / microsoft/TypeScript

Simpler Expression of Complex Type Conditions

Open
#41,123 3 comments 6 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
2d 4h
Merged PRs (30d)
132

Description

Search Terms

Multiple, type, condition, expression, no, extra, case, and, or

Suggestion

I often find myself repeating sub-branches of conditional types, which makes reasoning about control flow more difficult. I'd recommend the same logical operators to those of JS land, at the type-level.

Example

Let's say we have three boolean types––ConditionA, ConditionB and ConditionC––and we want to gather the number of true conditions in a new type Result, which will evaluate to 0 | 1 | 2 | 3 depending on the aforementioned conditions. The runtime equivalent can be expressed quite legibly:

const Result =
  (conditionA && conditionB && conditionC)
    ? 3
    : (
        (conditionA && conditionB) ||
        (conditionB && conditionC) ||
        (conditionA && conditionC)
      )
      ? 2
      : (conditionA || conditionB || conditionC)
        ? 1
        : 0;

The type-level equivalent is not as obvious in its meaning, and we end up repeating the potential results.

type Result =
  ConditionA extends true
    ? ConditionB extends true
      ? ConditionC extends true
        ? 3
        : 2
      : ConditionC extends true
        ? 2
        : 1
    : ConditionB extends true
      ? ConditionC extends true
        ? 2
        : 1
      : ConditionC extends true
        ? 1
        : 0;

While it nets more code, the following is––imo––easier to understand.

type Result =
  (ConditionA extends true && ConditionB extends true && ConditionC extends true)
    ? 3
    : (
        (ConditionA extends true && ConditionB extends true) ||
        (ConditionB extends true && ConditionC extends true) ||
        (ConditionA extends true && ConditionC extends true)
      )
      ? 2
      : (ConditionA extends true || ConditionB extends true || ConditionC extends true)
        ? 1
        : 0;

Ideally, the same rules from JS-land could apply to judging whether a type is a subtype of truthy, so we could do this:

type Result =
  (ConditionA && ConditionB && ConditionC)
    ? 3
    : (
        (ConditionA && ConditionB) ||
        (ConditionB && ConditionC) ||
        (ConditionA && ConditionC)
      )
      ? 2
      : (ConditionA || ConditionB || ConditionC)
        ? 1
        : 0;

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

Start by reviewing the conditional-type examples and the proposed && and || syntax in the issue. No source files, tests, or compiler entry points are named; done would require resolving the language-design question and validating the feature in the TypeScript compiler and its tests.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.