10 11 // ふたつの集合の和集合を表現するユニオン型と、 // ふたつの型の共通部分を表現するインターセクション型 type A = { a: string }; type B = { b: number }; // AとBの和集合を表現する型 type Union = A | B; // AとBの共通部分を表現する型 type Intersection = A & B; 出典:型のメンタルモデル | TypeScript入門『サバイバルTypeScript』 https://typescriptbook.jp/reference/values-types-variables/mental-model-of-types
11 12 13 // 代入可能 const w: WidgetCode = "W1234" as WidgetCode; // 代入可能 const fake: GizmoCode = "banana" as GizmoCode; // 代入不可能 const g: GizmoCode = w; // error TS2322: Type 'WidgetCode' is not assignable to type 'GizmoCode'. // Type 'WidgetCode' is not assignable to type '{ readonly __brand: "GizmoCode"; }'. // Types of property '__brand' are incompatible. // Type '"WidgetCode"' is not assignable to type '"GizmoCode"'.