Skip to content

Experiment short-circuiting of union distributionΒ #47511

Closed
@RyanCavanaugh

Description

@RyanCavanaugh

Suggestion

πŸ” Search Terms

union distribute short-circuit performance

βœ… 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

@amcasey and I were discussing #47481 and he had an interesting observation. Consider this program:

type HugeUnion1 = "a" | "b" | "c" | "d" | ... [10,000 more];
type HugeUnion2 = "q" | "r" | "s" | "t" | ... [10,000 more];

type Square<T, U> = T extends U ? true : false;

type M = Square<T, U>;

Naively, when evaluating M, we need to check if every member of HugeUnion1 is present in HugeUnion2. However, let's say we saw this (in the sorted form):

type HugeUnion1 = "a" | "q" | ... [10,000 more];
type HugeUnion2 = "a" | ... [10,000 more which do not include 'q'];

After evaluating "a", we have a true. After evaluating "q", we have a false. We can skip the rest of the list since no subsequent result will change the answer (because T does not appear in the conditional result)

Moreover, if instead of false we had never, we could immediately stop after seeing true, since true | never is just true

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions