Typescript
// Generic
function getSize<T>(arr: T[]): number {
return arr.length;
}
const arr1 = [1, 2, 3];
getSize<number>(arr1);
const arr2 = ["1", "2", "3"];
getSize<string>(arr2);
interface Mobile<T> {
name: string;
price: number;
option: T;
}
const m1: Mobile<{ color: string; coupon: boolean; }> = {
name: "s21",
price: 1000,
option: {
color: "red",
coupon: false,
}
}
const m2: Mobile<string> = {
name: "s20",
price: 900,
option: "good"
}
interface User {
name: string;
age: number;
}
interface Car {
name: string;
color: string;
}
const user: User = { name:"a", age:10 };
const car: Car = { name:"bmw", color:"red" };
function showName<T extends { name: string }>(data: T): string {
return data.name;
}
showName(user);
showName(car);
'Dev > Typescript' 카테고리의 다른 글
타입스크립트 Typescript 클래스 (0) | 2023.11.17 |
---|---|
타입스크립트 Typescript 함수 (0) | 2023.11.17 |
타입스크립트 Typescript 인터페이스 (0) | 2023.11.17 |
타입스크립트 Typescript 기본 타입 (1) | 2023.11.17 |