Skip to content

Генерация isolated declarations

Трансформер Oxc умеет генерировать декларации TypeScript без компилятора TypeScript для проектов с включённым isolatedDeclarations.

Пример

Вход:

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); // the .d.ts content
console.log(result.map); // the source map (if sourcemap is enabled)
console.log(result.errors); // parse and transformation errors

Также доступен синхронный вариант isolatedDeclarationSync с тем же набором параметров.