This is the cleanest way to make a JS library importable to a TS project. This approach is similar to Svelte's approach.
Note the dev dependency on TS is only used for `.d.ts` generation. The source code itself still does not use TS (or any dependencies other than Node.js) at all.
--- /dev/null
+node_modules
+types
\ No newline at end of file
"node": ">=20"
},
"files": [
- "src/*"
+ "./types/*",
+ "./src/*"
],
- "exports": "./src/index.js",
+ "exports": {
+ ".": {
+ "types": "./types/index.d.ts",
+ "default": "./src/index.js"
+ }
+ },
"scripts": {
+ "types": "tsc ./src/index.js --allowJs --declaration --emitDeclarationOnly --declarationMap --outFile ./types/index.d.ts",
"test": "node --test --test-skip-pattern=\"/^large:/\"",
"test:large": "node --test --test-name-pattern=\"/^large:/\"",
"test:coverage": "npm run test -- --experimental-test-coverage",
"bench": "node ./benchmark/core.bench.js"
+ },
+ "devDependencies": {
+ "typescript": "^5.8.3"
}
}