インターセクション型

二つの型を結合したい場合は、インターセクション型を使用します。BaseとProductというオブジェクトの型エイリアスがある場合、 Base & Productと宣言します。

index.ts◎
...
let num1: 10 | 20 = 10;

type Base = {
  id: number;
  createdAt: Date;
  updatedAt: Date;
};

type Prodct = {
  name: string;
  price: number;
};

let product1: Base & Prodct = {
  id: 1,
  name: "テレビ",
  price: 100000,
  createdAt: new Date(),
  updatedAt: new Date(),
};

Last updated