Skip to content

TypeScript Declarations Generator — Numio

Emits a .d.ts declaration file from TypeScript source, as tsc --declaration --emitDeclarationOnly does. Function bodies and initializers are removed, and private class members keep only their name.

You want to preview the public type surface a library file will publish before you build the package.

TypeScript

TypeScript

When to use it

  • You want to preview the public type surface a library file will publish before you build the package.
  • You need a hand-maintained .d.ts for a single module, or a declaration-only input for the TypeScript to Flow tool.

How to use

  1. Paste a TypeScript module into the TypeScript panel.
  2. The code is posted to /api/flow-to-typescript with isTS and declarationOnly set, and the TypeScript compiler emits declarations on the server.
  3. Copy the declarations into a .d.ts file. The page formats them with Prettier, so they use two-space indentation and no semicolons.

TypeScript

export interface CartItem { sku: string; price: number; qty: number }
export const TAX_RATE = 0.08;

export function subtotal(items: CartItem[]) {
  return items.reduce((sum, i) => sum + i.price * i.qty, 0);
}

export class Cart {
  private items: CartItem[] = [];
  constructor(public readonly currency = "USD") {}
  add(item: CartItem) {
    this.items.push(item);
    return this;
  }
}

TypeScript declarations (.d.ts)

export interface CartItem {
  sku: string
  price: number
  qty: number
}
export declare const TAX_RATE = 0.08
export declare function subtotal(items: CartItem[]): number
export declare class Cart {
  readonly currency: string
  private items
  constructor(currency?: string)
  add(item: CartItem): this
}

Details and limitations

The page sends your input to a server route (/api/flow-to-typescript) for conversion; it is not processed only in the browser. Next.js API routes accept request bodies up to 1 MB by default.

The compiler loads TypeScript's ES2020 and DOM library files, so Promise, Date, Map and return types inferred from built-in methods (number for subtotal in the example) are written correctly.

Syntax errors stop the conversion, and the compiler message, for example TS1134 "Variable declaration expected", is shown instead of output.

Each input is compiled as one file; imports from other modules are not resolved.

FAQ

Are async functions supported?

Yes. The inferred return type is wrapped in Promise, so an async function returning Todo[] is declared as export declare function loadTodos(url: string): Promise<Todo[]>.

What happens to private members?

They are kept by name only, for example "private items;", so the class shape still matches without exposing the type.

Are default parameter values kept?

No. A parameter with a default becomes optional, so constructor(public readonly currency = "USD") becomes constructor(currency?: string).

Specifications and references