document.querySelector() 메서드 사용하기

다양한 엘리먼트 타입으로 선언되기 때문에 타입을 따로 추론을 해줘야 한다. 그냥 메서드를 사용하니 경고가 나오는데 다음과 같이 해결할 수 있다.

const $root = document.querySelector('#root') as HTMLElement
console.log($root)

출처: https://bobbyhadz.com/blog/typescript-queryselector

Object 타입의 객체 사용하기

범용적으로 사용할 수 있는 타입인 Record다.
애매하면 사용하기 딱 좋다.

// variable
const foo:Record<string|number, any> = { foo: 'bar' }

// global..d.ts
declare global {
  export type Z = Record<string|number, any>
}

메타데이터

조회수
308
좋아요
0

0개의 댓글

등록된 댓글이 없습니다.