microsoft / microsoft/TypeScript
Type Alias Record/Interface Selector Pattern - Discriminated Unions, Fails to discriminate type shape, like switch does
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
It would be great, if this pattern would work, because it is so much simpler to use and would be so much faster typically than extends ? pattern, because there is no interrogation of the types with all complex patterns and shapes and forms, which gets quite hairy, performance slow downs major. With this pattern it just becomes iterative selection, hopefully just dictionary as is javascript in the backing the code. It would also ensure that everything is typechecked correctly for all of these interesting ways to use typescript.
typescript@3.3.0-dev.20181128
type TSPrimatives = boolean | number | string | Date;
type TSShapes = ShapeTsType<any> | ShapeTsRecord<any> | ShapeTsArrayPrimative<any> | ShapeTsArrayRecord<any>;
interface ShapeTsType<T extends TSPrimatives>
{
__Type : T
__Shape: 'T'
}
interface ShapeTsRecord<T extends Record<string, TSShapes>>
{
__Record : T
__Shape: 'R'
}
interface ShapeTsArrayPrimative<T extends TSShapes>
{
__ArrayPrimative : {w:T}
__Shape:'AP'
}
interface ShapeTsArrayRecord<T extends Record<string,TSShapes>>
{
__ArrayRecord : T
__Shape:'AR'
}
type ExtractShape<T extends ShapeTsType<any>> = T['__Type']
type ExractTsShape<T extends TSShapes> = ({
'T' : ExtractShape<T> // Fails to discriminate the type
'R' : T['__Record'] // Fails to discriminate the type
'AP' : T['__ArrayPrimative'] // Fails to discriminate the type
'AR' : T['__ArrayRecord'] // Fails to discriminate the type
})[T['__Shape']]
type ExtractRecordTSShape<T extends Record<string, TSShapes>> =
{
[K in keyof T] : ExractTsShape<T[K]>
}
https://www.typescriptlang.org/docs/handbook/advanced-types.html
Discriminated Unions
You can combine singleton types, union types, type guards, and type aliases to build an advanced pattern called discriminated unions, also known as tagged unions or algebraic data types. Discriminated unions are useful in functional programming. Some languages automatically discriminate unions for you; TypeScript instead builds on JavaScript patterns as they exist today. There are three ingredients:
Types that have a common, singleton type property — the discriminant.
A type alias that takes the union of those types — the union.
Type guards on the common property.
interface Square {
kind: "square";
size: number;
}
interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}
interface Circle {
kind: "circle";
radius: number;
}
First we declare the interfaces we will union. Each interface has a kind property with a different string literal type. The kind property is called the discriminant or tag. The other properties are specific to each interface. Notice that the interfaces are currently unrelated. Let’s put them into a union:
type Shape = Square | Rectangle | Circle;
Now let’s use the discriminated union:
function area(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.height * s.width;
case "circle": return Math.PI * s.radius ** 2;
}
}
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先复现 TypeScript 3.3.0-dev.20181128 示例,并将类型别名选择器模式与文档中的判别联合 switch 示例进行比较。该 issue 未指定源文件或测试;要视为完成,需要达成一致的实现,并验证选择器能够正确判别联合中的每个成员。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100