'What to put in package.json types field for typescript based libs
I'm a bit confused as to how best develop multiple typescript modules in parallel with code navigation and still publishing the right way. So what should I really put in package.json "types" field?
According to: Typescriptlang.org/publishing.html
I should put a reference to my generated index.d.ts like so:
{
"name": "awesome",
"author": "Vandelay Industries",
"version": "1.0.0",
"main": "./lib/main.js",
"types": "./lib/main.d.ts"
}
If I then develop a module in parallel that depends on this one with npm link, code navigation in for example vscode makes me just jump into this definitions file. Which is not what I want.
I want to go into the source file to be able to edit in the dep in parallel. No tsconfig setting with sourcemaps, inlined or not have helped in this regard. I might be missing something here. The only way I've managed my work flow to work decently is to actually point to the source main.ts file instead:
{
"name": "awesome",
"author": "Vandelay Industries",
"version": "1.0.0",
"main": "./lib/main.js",
"types": "./src/main.ts"
}
That however would make thing break when published right ?
At least if I put src under .npmignore. I'm failing to understand the best way to have a nice workflow with multiple typescript modules.
I mean, I wouldn't want to have to mangle package.json as part of release process…?
Solution 1:[1]
Here is a lighter weight alternative but it requires using PNPM. In your package02 add this to your package.json:
"type": "module",
"exports": {
".": "./src/index.ts"
},
"types": "./src/index.ts",
"publishConfig": {
"exports": {
".": "./lib/index.js"
},
"types": "./lib/index.d.ts"
},
When your package is published, the publishConfig options will override the development references in src.
Note, I'm building an ESM package with this solution. Not sure about CJS.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ben Stickley |
