> For the complete documentation index, see [llms.txt](https://organization-bht.gitbook.io/it/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://organization-bht.gitbook.io/it/undefined-1/react-with-typescript/type-script/3./undefined.md). Agents can also search and read this documentation through its [MCP server](https://organization-bht.gitbook.io/it/~gitbook/mcp).

# 참고) 타입선언파일

## 1. 타입 선언 파일(.d.ts)

* **Javascript 모듈의 타입설명서**

타입 선언 파일(`xxx.d.ts`)은 **JavaScript 코드의 타입 정보를 TypeScript에게 알려주는 파일**입니다.\
TypeScript는 **정적 타입 언어**이기 때문에, JavaScript처럼 동적으로 동작하는 코드의 타입을 알 수 없습니다.&#x20;

따라서 TypeScript기반프로젝트에 추가한  JS의 모듈은별도의 **타입검사용 파일(.d.ts)이 필요합니다**.&#x20;

즉, `.d.ts`파일은 Javascript사용을 위한 타입설명서라고 보시면 되겠습니다.  `.d.ts` 파일에는 **함수, 변수, 클래스**의 각 타입들이 포함되어 있으며 TypeScript는 이 파일을 통해 타입검사를 진행합니다.

```typescript
// my-math.js (자바스크립트 코드)
export function add(x, y) { // 매개변수의 타입 및 반환타입 알 수 없음
  return x + y;
}
export function multiply(x,y) {
  return x * y;
}

// math.d.ts
declare module "my-math" { // my-math모듈에 대한 타입 설명서. js의 모듈에 설명을 추가한다.
  export function add(x: number, y: number): number;
  export function multiply(x: number, y: number): number;
}
// app.ts
import { add } from "my-math"; 

// 컴파일시d.ts파일로 타입검사 진행.
// 런타임시 my-math.js의 add함수를 가져와서 실행
const result = add(2, 3); // 정상 작동 (타입도 인식됨)
```
