d.ts

김범주·2022년 7월 18일
0

typescript

목록 보기
19/23
post-thumbnail
//xxx.d.ts
export type Age = number;
export type multiply = (x :number ,y :number) => number
export interface Person { name : string }

xxx.d.ts 라고 작성하신 파일은 타입 정의만 넣을 수 있음
함수의 경우 함수에 { } 중괄호 붙이기는 불가능, 파라미터 & return 타입만 지정가능

정의해둔 타입은 export 해야만 사용가능
d.ts 파일은 ts 파일이 아니기 때문에 그냥 써도 ambient module이 되지 않음

(tsconfig.json)

{
    "compilerOptions": {
        "target": "es5",
        "module": "es6",
        "declaration": true,
    }
}

declaration 옵션을 true로 해두면 저장시 자동으로 ts파일마다 d.ts 파일이 옆에 생성됨

프로젝트 내에 types/common 이런 폴더 두개를 만들고
tsconfig.json 파일에 "typeRoots": ["./types"] 를 추가
ts 파일 작성할 때 타입없으면 자동으로 여기서 타입 찾아서 적용

profile
개발꿈나무

0개의 댓글