반응형
interface Student {
name: string;
}
//Student name 속성을 복사해 붙여넣는다고 생각하면 좋다. 해당하는 타입 설정해놓은 값을 쓸 수 있다.
interface IProps extends Student {
age: number;
}
let student: Student = { name: 'yo' };
let teacher: IProps = { name: 'yo', age: 30 };
type 과 interface의 차이점은?
interface는 중복 선언이 가능하다. 하지만 type은 불가능하다.
즉 합쳐진다. 하지만 type은 엄격해서 중복시 중복되었다고 에러메시지가 뜬다.
interface는 외부 라이브러리가 interface를 많이 사용하는데, 타입이 불만족스러울 때, 커스텀마이징이 가능하다.
interface Student {
name: string;
}
interface Student {
score: string;
}
만약 & 연산자로 합치면?
interface Student {
name: string;
}
interface IProps {
name: number;
}
let foo: Student & IProps = { name : "yo" }
중복되지만, 에러가 난다. type 키워드 또한 같은 현상이 발생한다.
만약 둘 다 name이 같은 타입이라면 에러가 나는게 아니라, 하나로 합쳐준다.
반응형
'TypeScript' 카테고리의 다른 글
TS에서 type과 interface의 차이점은? (0) | 2022.06.16 |
---|---|
react에서 type 설정 하는 방법 (0) | 2022.05.13 |
타입스크립트로 HTML 변경과 조작할 때 주의점들 (0) | 2022.05.12 |
타입도 변수에 담아쓴다. type 키워드 써서 & readonly, Literal Types로 만드는 const 변수 유사품 (0) | 2022.05.11 |
타입 확정하기 Narrowing & Assertion (0) | 2022.05.11 |