babel-dts-generator
raw JSON → 0.6.3 verified Sat Apr 25 auth: no javascript maintenance
A Babel plugin that generates TypeScript declaration (.d.ts) files from JavaScript code with JSDoc type annotations. Version 0.6.3 is the latest stable release; the project appears to be in maintenance mode with no recent activity. It specializes in converting Babel-parsed ASTs into TypeScript declarations, supporting features like object parameters, this-type annotations, typeof-type annotations, and string literal types. Key differentiators include integration with Babel's transformation pipeline and suppression of private exports for clean typings.
Common errors
error ReferenceError: regeneratorRuntime is not defined ↓
cause Async/await usage without regenerator runtime polyfill
fix
Install regenerator-runtime and require('regenerator-runtime/runtime') at your entry point
error Cannot find module 'babel-core' ↓
cause babel-core not installed or wrong version (needs v6)
fix
Run npm install babel-core@^6.0.0 --save-dev
error TypeError: generator is not a function ↓
cause Using default import incorrectly with CommonJS require
fix
Use const generator = require('babel-dts-generator').default; in CommonJS
Warnings
breaking Untyped parameters now treated as optional (since v0.5.0) ↓
fix Add explicit type annotations to parameters that should be required, or update code to handle optional parameters
gotcha Requires babel-core (not @babel/core) due to legacy Babel 6 plugin API ↓
fix Ensure you have babel-core@^6.x installed, not @babel/core
deprecated The project has not been updated since 2017 and may not work with modern Babel versions ↓
fix Consider alternatives like tsc or @babel/preset-typescript for generating .d.ts files
gotcha Supports only default exports? Check configuration for named exports ↓
fix Verify the 'output' option points to a folder; plugin generates .d.ts for each file but may require additional config
breaking Private exports (prefixed with _) are suppressed by default since v0.6.0 ↓
fix If you need _-prefixed exports in .d.ts, adjust the 'suppressPrivate' option (not documented)
gotcha Generator yields a Promise, not synchronous result ↓
fix Always await or use .then() when calling babel.transform() with this plugin
Install
npm install babel-dts-generator yarn add babel-dts-generator pnpm add babel-dts-generator Imports
- default wrong
const generator = require('babel-dts-generator').defaultcorrectimport generator from 'babel-dts-generator' - generate
import { generate } from 'babel-dts-generator' - BabelDtsGeneratorOptions wrong
import { BabelDtsGeneratorOptions } from 'babel-dts-generator'correctimport type { BabelDtsGeneratorOptions } from 'babel-dts-generator'
Quickstart
import generator from 'babel-dts-generator';
import * as babel from 'babel-core';
const code = `
/** @param {number} a */
function foo(a) { return a; }
`;
babel.transform(code, {
plugins: [
[generator, {
output: 'dist/types',
suppressExcess: true,
suppressAmbientDeclaration: true
}]
]
}).then(result => {
console.log(result.code); // .d.ts content
}).catch(err => console.error(err));