microsoft / microsoft/TypeScript
Literal Comparison for Conditional Types
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
Search Terms
compare type to value literal, conditional type literals, type is literal, complex schemas
Suggestion
The ability to compare a type against an example of that type, for use in complex type aliases and interfaces.
Use Cases
When designing a complex schema for an object, the actual fields contained by the object can branch in many different ways.
Of course, it's possible to approach the problem by using nullable types for fields that are not present in all cases. This results in a readable type in the error inspection window, but lots of unnecessarily defensive code that would have been solved by a fully typed schema.
It's also possible to create a discriminated union consisting of every branch--but what if you have a lot of branches? It becomes undesirably repetitive and the error inspection window is a wall of text.
Examples
Take for example, the type of a Pipeline:
type Action = 'Data Selection' | 'Data Cleanup' | 'Train' | 'Store' | 'Deploy'
type Status = 'running' | 'finished' | 'error'
type PipelineStep = {
name: Action
status: Status
tags: Array<string>
// `never` actually always results in a type check error right now
message: PipelineStep['status'] is 'Error' ? string : never
path: PipelineStep['name'] is 'Data Selection' | 'Store' ? string : never
trainingID: PipelineStep['name'] is 'Train' ? string : never
endpoint: PipelineStep['name'] is 'Deploy' ? string : never
}
Contrasted with the current syntax and discriminated unions:
type PipelineStep = {
name: Action
status: Exclude<Status, 'Error'>
tags: Array<String>
} | {
name: Action
status: 'Error'
tags: Array<string>
message: string
} | // I give up
The number of branches is multiplicative in nature. In this case, the most complex schema could have a maximum of 6 (Actions) x 3 (Status) possible branches.
After optimization, it becomes:
type StepStatus = {
status: Exclude<Status, 'Error'>
} | {
status: 'Error'
message: string
}
type DataSelectionStep = {
name: 'Data Selection'
path: string
}
type TrainingStep = {
name: 'Training'
trainingID: string
}
type StoreStep = {
name: 'Store'
path: string
}
type DeployStep = {
name: 'Deploy'
endpoint: string
}
type PipelineStep = (DataSelectionStep | TrainingStep | StoreStep | DeployStep | {
name: Exclude<Action, 'Training' | 'Data Selection' | 'Store' | 'Deploy'>
}) & StepStatus & {
tags: Array<String>
}
which isn't really much better...
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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
没有指定文件、测试或入口点。首先查看负责条件类型的 TypeScript 类型检查器区域,并将该提案与现有的判别联合行为进行比较;完成的标准是提供一种受支持的类型到字面量比较语法,能够处理这些示例而不改变生成的 JavaScript。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100