I'm trying to generate type declarations for old dependencies I use, which emit CJS modules and have no typings of their own. For example, the aabb-3d module (though this issue isn't specific to that module).
I'm generating the declaration with a command like:
tsc node_modules/aabb-3d/index.js
--allowJs
--declaration
--emitDeclarationOnly
--outFile types/aabb-3d/index.d.ts
This works, and the declaration gets generated, but the contents of the file look like:
declare module "index" {
export = AABB;
function AABB(pos: any, vec: any): AABB;
//...
When I then try to edit code, my editor doesn't pick up the typings because it expects the declaration to be declaring the module aabb-3d
, not index
.
If I manually change the generated d.ts
file, my editor works and code hints work correctly for the legacy module. How can I get tsc
to generate working declaration files?
I checked TypeScript's own code, and there's no evident way to change how the
declare module
line is generated. Fortunately, you can just prevent it from being generated at all. You're getting an ambient external module declaration (declare module
) because you're using--outFile
instead of--outDir
. Here's how to generate the sametypes/aabb-3d/index.ts
file as an ES-style module declaration withoutdeclare module
:In order for TypeScript to find the ES-style module declarations in the
types
tree, you'll need the following in your project'stsconfig.json
:Then you can
import aabb3d from 'aabb-3d'
in your code with the typing in place, such as it is when tsc auto-generates it.In case a package's
"main"
file is not namedindex.js
and thus the generated declarations are not namedindex.d.ts
, you will need to put apackage.json
file in thetypes/$package
directory to point the compiler to the right file. For example, if the main file weremain.js
, making the declarations filemain.d.ts
, thepackage.json
would be: