microsoft / microsoft/TypeScript

Custom syntax for distributive conditional

未关闭
#63,295 9 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔍 Search Terms

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

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ 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

```typescript
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: 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.{}
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.
3. 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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先查看 issue 中描述的动机示例和现有的分配条件语法。比较提议的 Syntax1 和 Syntax2 形式,包括它们对分配范围的处理方式以及可选配置这一想法。达成一致的具体语法和相应的 TypeScript 类型检查行为后,即可视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
需要澄清
新手友好度
28/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。