크게 다르지 않아서 소스코드만 첨부하겠습니다.
//제네릭 클래스
class User<P> {
constructor(public payload: P) {}
getPayload() {
return this.payload;
}
}
interface UserAType {
name: string;
age: number;
isValid: boolean;
}
interface UserBType {
name: string;
age: number;
emails: string[];
}
const jplum = new User<UserAType>({
name: "J-plum",
age: 53492,
isValid: true,
emails: [], //에러 발생 UserAType 에는 emails가 없음
});
const euang = new User<UserBType>({
name: "Eunag",
// age가 없어서 에러 발생
emails: ["euang@gamil.com"],
});
'JavaScript > TypeScript' 카테고리의 다른 글
[TypeScript] 추론과 명시적인 타입 지정.? (0) | 2023.04.10 |
---|---|
[TypeScript] 제네릭 - 인터페이스, 제약 조건 (0) | 2023.04.10 |
[TypeScript] 제네릭(Generic) - 함수 (0) | 2023.04.10 |
[TypeScript] 클래스와 접근 제어자 (0) | 2023.04.10 |
[TypeScript] 오버로딩 (1) | 2023.04.10 |