镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Experiment short-circuiting of union distribution #47511

Closed

Description

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

Andrew Casey (@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

Activity

  1. DanielRosenwasser commented on Jan 20, 2022

    @DanielRosenwasser
    Member

    Seems like this would use similar rules as Anders Hejlsberg (@ahejlsberg)'s #46429, right? You can avoid distribution and just map to a set of possibilities for A extends B ? C : D when

    • The check-type (A) appears in neither branch and
    • The extends type (B) has no infer positions

    With the possibilities never, C, D, C | D, and possibly any (?)

  2. jakebailey commented on Mar 18, 2023

    @jakebailey
    Member

    With #53192 merged, I think that this one is effectively "done"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions