Skip to content

隔离声明文件生成

Oxc transformer 支持为启用了 isolated declarations 的项目生成 TypeScript 声明文件,无需使用 TypeScript 编译器。

示例

输入

ts
export function foo(a: number, b: string): number {
  return a + Number(b);
}

export enum Bar {
  a,
  b,
}

输出

ts
export declare function foo(a: number, b: string): number;
export declare enum Bar {
  a = 0,
  b = 1,
}

用法

ts
import { isolatedDeclaration } from "oxc-transform";

const result = await isolatedDeclaration("lib.ts", sourceCode, {
  sourcemap: false,
  stripInternal: false,
});

console.log(result.code); // .d.ts 内容
console.log(result.map); // source map(如果启用了 sourcemap)
console.log(result.errors); // 解析和转换错误

也提供了相同签名的同步版本 isolatedDeclarationSync