From 66164961f4bf5df5b49652f09509c6f62ed7d4e8 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 29 Apr 2025 22:05:31 -0700 Subject: [PATCH] Add TypeScript declaration file (`.d.ts`) generation 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. --- .gitignore | 2 ++ package.json | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d75092 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +types \ No newline at end of file diff --git a/package.json b/package.json index a986620..36dfb7f 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,23 @@ "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" } } -- 2.47.3