JSTS

[TS] 실습 3. 외부 라이브러리 모듈화

Jaaaay 2022. 7. 26. 14:00

Definitely typed

타입 definition 라이브러리와 패키지 타입 검색기

https://github.com/DefinitelyTyped/DefinitelyTyped

Search for typed packages

타입 선언 라이브러리가 제공되지 않는 외부 라이브러리 대처 방법

  1. tsconfig.json에 typeRoots 추가

따로 지정해주지 않은 때는 typeRoots가 기본적으로 node_modules/@types 를 가르킴

{
  "compilerOptions": {
    "allowJs": true,
    "target": "ES5",
    "outDir": "./built",
    "moduleResolution": "Node",
    "lib": ["ES2015", "DOM", "DOM.Iterable"],
    "noImplicitAny": true,
    "typeRoots": ["./node_modules/@types", "./types"]
  },
  "include": ["./src/**/*"]
}

따로 폴더를 만들고 경로를 추가로 지정

   2. 정의한 폴더에 해당 라이브러리 폴더와 파일 생성

파일 명은 원하는 라이브러리 폴더명 안에 index.d.ts 로 생성

// index.d.ts 예시
declare module 'chart.js' {
  // ...
}