generated at
Tagged Union
Union型を定義する際に、
個々の型を識別する用のtagを付けるやつ
タグとして使われるのでよく見るのは、 kind とか tag とか
fp-tsでは _tag を使っているねmrsekut
TS上でパターンマッチを模倣するための、TS固有の手段とも言える


ts
type Square = { kind: "square"; // これ size: number; } type Rectangle = { kind: "rectangle"; // これ width: number; height: number; } type Shape = Square | Rectangle;



TypeScript v4.5からTemplate Literal Typesをkindに使える
ex.ts
export interface Success { type: `${string}Success`; body: string; } export interface Error { type: `${string}Error`; message: string; } export function handler(r: Success | Error) { if (r.type === "HttpSuccess") { // 'r' has type 'Success' let token = r.body; } }




日本語でなんと呼ぶか問題
無理に日本語で呼ばなくて良い気もするmrsekut