TS 类型声明文件的一般写法

11 min read

TS 类型声明文件通常以 .d.ts 后缀结尾,用于描述 JavaScript 库或模块的类型信息

// math.d.ts

declare module 'math' {
  export function add(a: number, b: number): number;
  export function subtract(a: number, b: number): number;
}

使用

// app.ts

import { add, subtract } from 'math';

const result1: number = add(1, 2);
const result2: number = subtract(4, 2);