Open1

プロを目指す人のためのTypeScript入門 メモ

ユウマユウマ

3.3部分型関係

以下のような場合、Human型はAnimal型の部分型となるので、helloFunctionの引数にhumanが入っても問題ない。

type Animal = {
    age: number
}

type Human = {
    age: number
    name: string
}

const human: Human = {
    age: 25,
    name: "yuma"
}

const helloFunction = (data:Animal) => {
    console.log("data", data)
}

helloFunction(human)