hapijs / hoek

Node utilities shared among the extended hapi universe
Other
481 stars 171 forks source link

Missing types in typescript with module resolution nodenext #385

Closed ZachHaber closed 8 months ago

ZachHaber commented 1 year ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch @hapi/hoek@11.0.2 for the project I'm working on.

I'm using typescript with the config "module": "NodeNext", which makes typescript very particular about the exports and types. Due to this, when I updated @hapi/hoek to the latest version (11.0.2) from version ^10, I started getting an error that I was missing the types for @hapi/hoek despite the types being in the package. This error is fixed by adding the index.d.ts type file to the main exports types field.

Then in the project you have to pull in Hoek as either individual named exports or using import * as Hoek from '@hapi/hoek'

Here is the diff that solved my problem:

diff --git a/node_modules/@hapi/hoek/package.json b/node_modules/@hapi/hoek/package.json
index ce58817..4436342 100644
--- a/node_modules/@hapi/hoek/package.json
+++ b/node_modules/@hapi/hoek/package.json
@@ -8,7 +8,8 @@
   "exports": {
     ".": {
       "import": "./lib/index.mjs",
-      "require": "./lib/index.js"
+      "require": "./lib/index.js",
+      "types": "./lib/index.d.ts"
     },
     "./applyToDefaults": "./lib/applyToDefaults.js",
     "./assert": "./lib/assert.js",

This issue body was partially generated by patch-package.

kanongil commented 1 year ago

I haven’t looked at your proposed solution, but I can confirm the issue.

ZachHaber commented 1 year ago

If anyone else is having this issue in the meantime, instead of using patch-package, you can also import the sub-modules directly (which does use commonjs, but the interop doesn't have an issue here) e.g.

import clone from '@hapi/hoek/clone'

It's a little more annoying and doesn't have the auto-import functionality that the main import does, but it won't cause type issues.

Nargonath commented 8 months ago

Fix published in v11.0.4.