備忘録として TypeScriptのインプットをまとめていく.
Objectの構造を定義するもの
type alias, interface, class ってやつがある。
前回は Interface だった。
今回はTypeをキャッチアップ
Type
正確には Type Alias(型エイリアス) って名前
type StrOrNum = string | number;
let sample : StrOrNum;
sample = 1
sample = "test"
また、前回の Interfaceのように宣言する事も可能
type Item = {
name: string;
price: number;
}
上記の宣言を使用して、同様に使用する事ができる
const pencil: Item = {
name: "鉛筆",
price: 110
}
Type Alias と Interface の違い
Type Alias は Interfaceのように拡張したり、実装したり出来ないという事に違いがある
これとか腹落ちしやすかった