Skip to content

Custom syntax for distributive conditionalΒ #63295

@yarden-app

Description

@yarden-app

πŸ” Search Terms

Distributive conditional, large type with numerous(10,20+) union types limitation, union type aggregation

βœ… Viability Checklist

⭐ Suggestion

My suggestion is to improve the current syntax of the awesome 'distributive conditional' feature.
This feature is pretty much hidden due to the verbosity required to use it and the inability to reason with the syntax.

My example may show an idea for how a syntax may look, it isn't covering everything (like for example the scope of distribution like T extends {SCOPE}, typically 'any')

πŸ“ƒ Motivating Example

type A = {
  A1: {
    Type: "type.1";
    Sub: {
      Type: "type.1.sub.1" | "type.1.sub.2"
    };
  };
  A2: {
    Type: "type.2";
    Sub: {
      Type: "type.2.sub.1" | "type.2.sub.2"
    };
  }
}

// With generics it is simple
export namespace generic_example {
  type B<MainType extends A[keyof A]["Type"]> = {
    MainType: MainType;
    SubType: (A[keyof A] & {Type: MainType})["Sub"]["Type"]
  };

  // Results in a safe type when given a single type
  const b:B<"type.1"> = {
    MainType: "type.1",
    SubType: "type.1.sub.1"
  }
}

// Just a demonstration of a generally invalid example
export namespace invalid_example {
  type B = {
    MainType: A[keyof A]["Type"];
    SubType: A[keyof A]["Sub"]["Type"]
  };

  // As expected, there are no type error, even though type.1.sub.1 is not part of type.2
  const b:B = {
    MainType: "type.2",
    SubType: "type.1.sub.1"
  }
}

// With distributive conditional
export namespace wild_syntax_example {
  type theUnion = A[keyof A];

  type B = theUnion extends infer U ? U extends {Type: any; Sub: {
    Type: any
  }} ? {
    MainType: U["Type"];
    SubType: U["Sub"]["Type"]
  } : never : never;

  const b:B = {
    MainType: "type.1",
    SubType: "type.1.sub.1"
  }

  const b_invalid:B = { //<-- Correctly guards the type, but can you really work like this?
    MainType: "type.2",
    SubType: "type.1.sub.1"
  }
}

// Some ideas for syntax that can simplify it
export namespace suggested_syntax {
  // You wont have access to the main A anymore but adding optional alias can solve it like A.<A_DISTRIBUTED>{}
  type Syntax1 = A.{
    MainType: A["Type"];
    SubType: A["Sub"]["Type"];
  }

  // Similar to destructure which could fit here nicely
  type Syntax2 = {
    ...A {
      Type,
        Sub: {
          Type
      }
    }
  }
}

πŸ’» Use Cases

  1. What do you want to use this for?
    I think this is a missing/hidden piece in typescript and should be added as soon as possible. Please let me know if you think otherwise.
  2. What shortcomings exist with current approaches?
    Long, easy to forget, super messy syntax for very "simple" things ( at least in most of my use cases )

🍭 Bonus addition

  1. Optionally, add the option to specify something like proprietaryDistributiveConditional=true in tsconfig to allow to disable the current distributive conditional syntax so it is possible to use statements like T extends Union without distributing it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions