microsoft / microsoft/TypeScript

Type Alias Record/Interface Selector Pattern - Discriminated Unions, Fails to discriminate type shape, like switch does

オープン
#28,699 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
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;
}
}

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず TypeScript 3.3.0-dev.20181128 の例を再現し、型エイリアスセレクターのパターンを、文書化されている判別共用体の switch の例と比較します。この issue ではソースファイルやテストは指定されていません。完了とするには、合意された実装と、セレクターが各ユニオンメンバーを正しく判別することの検証が必要です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。