項目67 パブリックなAPIで使われるすべての型をエクスポートする
パブリックなAPI(メソッド)の一部として使われる型は、すべてエクスポートするべき
特にライブラリの作者は意識する必要がある
ただ、型がエクスポートされていなくても、関数から引数や戻り値の型を抽出することは可能
code:ts
interface SecretName {
first: string;
last: string;
}
interface SecretSanta {
name: SecretName;
gift: string;
}
export function getGift(name: SecretName, gift: string): SecretSanta {
// ...
}
code:ts
type MySanta = ReturnType<typeof getGift>;
// ^? type MySanta = SecretSanta
type MyName = Parameters<typeof getGift>0; // ^? type MyName = SecretName