Skip to main content

emit_isolated_declarations

Function emit_isolated_declarations 

Source
pub fn emit_isolated_declarations(
    source: &str,
    filename: &str,
) -> Result<String>
Expand description

Emit a TypeScript declaration file (.d.ts) from TypeScript source.

Uses oxc’s isolated-declarations transformer — no full type checker is required, but the source must follow TypeScript’s isolated declarations rules: every exported value needs an explicit type annotation.

Fresh runs this over every TypeScript plugin at load time so the plugin’s public types (anything the file exports plus any declare global / module-augmentation blocks) are available to downstream plugins and to the user’s init.ts without manual .d.ts maintenance.

The source is forced into module mode before the transform runs: isolated-declarations only hides non-exported symbols when the input AST has at least one ImportDeclaration/ExportDeclaration. For a plain-script input it instead emits a declare for every top-level declaration, leaking internal interfaces, constants, and function signatures into the aggregate plugins.d.ts. Many Fresh plugins have no import/export statement (they’re top-level calls on the ambient editor global), so we append the canonical empty-export marker export {}; when the source doesn’t already have module syntax. SourceType::with_module(true) on the parser alone is not enough — the transform looks at the AST, not the parser flag.

Returns the generated .d.ts source as a string. Non-fatal diagnostics from the isolated-declarations pass are surfaced in the error path when they render an empty emit unusable; benign diagnostics (e.g. “defaults exported without explicit types”) are tolerated and the caller simply gets a partial emit.