본문 바로가기
Javascript/Typescript

[Typescript] this 타입

by 같이긍뱅와 2023. 11. 12.
interface Book {
  name: string
}

function greet(this: Book, msg: string) {
  return `${this.name}, ${msg}`
}

 

TS 에서 this타입을 정의할 때 함수의 첫번째 인자로 this를 넣으면 this는 예약어이기 때문에 매개변수 중 하나로 인식을 안하고 this의 타입을 설정할 수 있다. 

댓글