Skip to content

Instantly share code, notes, and snippets.

@orta
Last active January 19, 2023 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orta/9d932ae27dfa12b44ca4c10775a3eff6 to your computer and use it in GitHub Desktop.
Save orta/9d932ae27dfa12b44ca4c10775a3eff6 to your computer and use it in GitHub Desktop.
Notes on adding JSDoc -> d.ts support for JS libs

Command to add:

yarn tsc --declaration --emitDeclarationOnly --allowJs --lib es2015 lib/*.js

or via npx

npx -p typescript@next tsc --declaration --emitDeclarationOnly --allowJs --lib es2015 lib/*.js

Exports for

/**
 * Query String
 *
 * @param {any} object An object
 */
var exportedStringify = function (object, opts) {}

module.exports = exportedStringify

Not as expected


Looks like overwriting the d.ts files is an issue when using emitDeclarationOnly:

 qs   master ● ?  npx -p typescript@next tsc --declaration --emitDeclarationOnly --allowJs --lib es2015 lib/*.js                                                                                       1 ↵  09:00:53
npx: installed 1 in 6.712s
error TS5055: Cannot write file 'lib/formats.d.ts' because it would overwrite input file.
  Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.

error TS5055: Cannot write file 'lib/parse.d.ts' because it would overwrite input file.
  Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.

error TS5055: Cannot write file 'lib/stringify.d.ts' because it would overwrite input file.
  Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.

error TS5055: Cannot write file 'lib/utils.d.ts' because it would overwrite input file.

emitDeclarationOnly is not in tsc --help


// @allowJs: true
// @checkJs: true
// @emitDeclarationOnly: true
// @declaration: true
// @

// @Filename: /a.js
/**
 * Query String
 *
 * @param {any} object An object
 */
var exportedStringify = function (object, opts) {}
module.exports = exportedStringify




//// [a.d.ts]
export function exportedStringify(object: any, opts: any): void;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment