[TypeScript]型エイリアス

型エイリアス

type DataType = number | string;
type EventName = "click" | "hover" | "mousedown";

※型エイリアスはブロックスコープ
 
使う時

let firstDataType : DataType = 123;

 
型エイリアスのユニオン

type Team = ATeam | BTeam;
type ATeam = "john" | "jeff" | "joe";
type BTeam = "mike" | "migera" | "mika";
let partner:Team = "mike";

 
オブジェクトの時
通常

let article : {
    id    : number;
    title : string;
    body  : string;
} = {
    id    : 875,
    title : "あいうえお",
    body  : "かきくけこ",
};

 
型エイリアス

type Article = {
    id    : number;
    title : string;
    body  : string;
};
const article : Article = {
    id    : 875,
    title : "あいうえお",
    body  : "かきくけこ",
};

 
ネストパターン1

type Order = {
    id : number;
    title : string;
    price : number;
    product : {
        title : string;
        price : number;
    };
};

 
ネストパターン2

type Product = {
    title : string;
    price : number;
};
type Order = {
    id : number;
    title : string;
    price : number;
    product : Product;
};