Object
ornull
null
일 경우
의미: 프로토타입 체인의 최상단
class Person {
constructor(name, age) {
this.name = name
this.age = age
}
}
class KMJ extends Person {
sayHi() {
console.log(`hi, ${this.name}`)
}
}
const mj = new KMJ('kmj', 10)
console.log(mj.sayHi()) // output: hi, kmj
class
의interface
에 만족하는지 여부를 체크할 때 사용
TS에서 interface
& class
동시 확장 가능
implements
한 interface
의 타입이 없다면 → 에러 반환
interface Person {
name: string
age: number
}
// 에러: age 미정의
class Howdy implements Person {
// Class 'Howdy' incorrectly implements interface 'Person'.
// Property 'age' is missing in type 'Howdy' but required in type 'Person'.
name = 'howdy'
}
오직 타입 체크를 위해 사용
안의 값을 자동으로 바꾸지 x
예시 코드
interface Person {
name: string
age: number
isMJ(name: string): boolean
}
class Howdy implements Person {
name = 'howdy'
age = 20
isMJ(name) {
// 에러: parameter의 타입 미지정
// Parameter 'name' implicitly has an 'any' type, but a better type may be inferred from usage.
return this.name === 'kmj'
}
}
참고