エイリアスとは
エイリアスとは、変数名やプロパティ名を他の名前で扱う構文です。
使われる場面
1.オブジェクトの分割代入でエイリアスを使う
const user = {
id: 1,
username: 'hana',
age: 20
};
// username → name に別名をつけて使う
const { username: name } = user;
console.log(name); // 'hana'
2.関数の引数でエイリアスを使う
const printUser = ({ username: name }: { username: string }) => {
console.log(name);
};
printUser({ username: 'taro' }); // 'taro'
3.型でエイリアスを使う
type UserId = number;
const getUser = (id: UserId): void => {
console.log("ID:", id);
};