Skip to content

Instantly share code, notes, and snippets.

@danieldietrich
Last active September 27, 2022 20:20
Show Gist options
  • Save danieldietrich/c0a21d28a52b05f307e4a456533de287 to your computer and use it in GitHub Desktop.
Save danieldietrich/c0a21d28a52b05f307e4a456533de287 to your computer and use it in GitHub Desktop.

Revisions

  1. danieldietrich revised this gist Sep 27, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 03_answer.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    βœ… Solved distribution:

    ```ts
    DistributiveConditionalType<never, 1> // why resolved as πŸ‘‰ never?
    = never // never is the empty union, it distributes to nothing and therefore resolves to never.
    DistributiveConditionalType<never, 1>
    = never // never is the empty union, it distributes to nothing and therefore the type resolves to never
    ```

    βœ… Solved the expanded type:
  2. danieldietrich revised this gist Sep 27, 2022. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  3. danieldietrich revised this gist Sep 27, 2022. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions answer.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    βœ… Solved distribution:

    ```ts
    DistributiveConditionalType<never, 1> // why resolved as πŸ‘‰ never?
    = never // never is the empty union, it distributes to nothing and therefore resolves to never.
    ```

    βœ… Solved the expanded type:

    ```ts
    (never) extends (1) ? true : false
    = true // no type extends anything by definition
    ```
  4. danieldietrich created this gist Sep 27, 2022.
    18 changes: 18 additions & 0 deletions definition.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    **Def.** A conditional type with a checked _naked type parameter_ T is called _distributive conditional types_.

    Example:

    ```ts
    // given U, X and Y
    type DistributiveConditionalType<T> = T extends U ? X : Y;
    ```

    Distributive conditional types are automatically distributed over union types during instantiation.

    Example:

    ```ts
    DistributiveConditionalType<A | B | C>
    = (A | B | C) extends U ? X : Y
    = (A extends U ? X : Y) | (B extends U ? X : Y) | (C extends U ? X : Y)
    ```
    13 changes: 13 additions & 0 deletions question.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    Given

    ```ts
    type DistributiveConditionalType<T1, T2> = T1 extends T2 ? true : false;
    ```

    Then

    ```ts
    DistributiveConditionalType<never, 1> // why resolved as πŸ‘‰ never?
    = (never) extends (1) ? true : false // why resolved as πŸ‘‰ true?
    = ??? // How does the πŸ‘‰ distribution look like?
    ```