LoginSignup
0
0

More than 3 years have passed since last update.

Typescript 奮闘記 No.02 『Type Alias編』

Posted at

備忘録として 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のように拡張したり、実装したり出来ないという事に違いがある

これとか腹落ちしやすかった

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0