객체의 값과 속성 타입 지정하기
type Height = number;
type Name = string;
interface IHeightRecord {
[name: Name]: Height;
}
type THeightRecord = {
[name: Name]: Height;
}
const heightRecord: IHeightRecord = {
'철수': 170,
'영희': 160,
'길동': 175
}
구체적으로 속성명과 그 속성값의 type을 정하는 것이 아니라
속성명의 type은 무엇이고, 속성값의 type은 무엇인지만 정의할 수도 있다.
interface와 접근제어자
interface IAnimal {
readonly name: string;
age: number;
}
type TAnimal = {
readonly name: string;
age: number;
}
class Tiger implements TAnimal {
// private name: string; // 에러 발생
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
type alias나 interface로 선언된 type을 class에서 implements로 구현 시
type에 선언된 필드나 함수는 반드시 접근제어자를 public으로 사용해야 한다.
private으로 사용하고싶다면 type에서 아예 해당 필드를 제거 후 사용해야 한다.
'JS&TS Essential > Type' 카테고리의 다른 글
Type의 확장 (0) | 2021.11.11 |
---|---|
Type 추론 (0) | 2021.11.09 |
type alias와 interface (0) | 2021.03.04 |
TypeScript의 type 알아보기 (0) | 2021.03.03 |