Can't use TS typedefs on an ESM setup
basicdays opened this issue · 9 comments
Describe the bug
I am not able to use the TypeScript type definition files when my Node 18 project is setup with ESM support enabled. TypeScript compilation will produce the following error:
Could not find a declaration file for module 'graphql-modules'. '/workspace/node_modules/graphql-modules/index.mjs' implicitly has an 'any' type.
There are types at '/workspace/node_modules/graphql-modules/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'graphql-modules' library may need to update its package.json or typings.
To Reproduce
Steps to reproduce the behavior:
package.json
(abbreviated):
{
"type": "module",
"devDependencies": {
"typescript": "~5.0"
}
}
tsconfig.json
(abbreviated):
{
"compilerOptions": {
"module": "node16",
"moduleResolution": "node16"
}
}
In any ts
file the following will produce the TypeScript error:
import { createModule, gql } from "graphql-modules";
Expected behavior
An import should use the expected type definition files.
Environment:
- OS: MacOS 12.6.6
@graphql-modules/...
:2.1.2
- NodeJS:
18.16.0
Additional context
Was able to find some documentation on how to reference typedefs in the new exports
feature: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing
For what it's worth looking at the current graphql-modules
package.json
file, it currently has the following:
{
"main": "index.js",
"module": "index.mjs",
"typings": "index.d.ts",
"typescript": {
"definition": "index.d.ts"
},
"exports": {
".": {
"require": "./index.js",
"import": "./index.mjs"
},
"./*": {
"require": "./*.js",
"import": "./*.mjs"
},
"./package.json": "./package.json"
}
}
The graphql
package at version 16.6.0
seems to be working correctly. It currently has the following:
{
"main": "index",
"module": "index.mjs",
"typesVersions": {
">=4.1.0": {
"*": [
"*"
]
},
"*": {
"*": [
"NotSupportedTSVersion.d.ts"
]
}
}
}
My current workaround is just to do the following when importing:
import graphqlModules = require("graphql-modules");
const { gql, createModule } = graphqlModules;
Good news, I edited the graphql-modules/package.json
file in my node_modules
directly and got it working with the following:
{
"main": "index.js",
"module": "index.mjs",
"typings": "index.d.ts",
"typescript": {
"definition": "index.d.ts"
},
"exports": {
".": {
"require": {
"types": "./index.d.ts",
"default": "./index.js"
},
"import": {
"types": "./index.d.ts",
"default":"./index.mjs"
}
},
"./*": {
"require": {
"types": "./*.d.ts",
"default": "./*.js"
},
"import": {
"types": "./*.d.ts",
"default": "./*.mjs"
}
},
"./package.json": "./package.json"
}
}
So I just revisted this again. Was going to do a PR to fix this, but I noticed that the package.json
in the source includes the proper typings: https://github.com/Urigo/graphql-modules/blob/master/packages/graphql-modules/package.json#L21
However the package.json
you get from the package published to npm is missing the exports["."].types
and exports["./*"].types
fields. I've used patch-package
to at least rectify this for now, but is this a bug in the publishing process that's changing them? Does the package just need to get republished to get the correct exports?
Here's the patch-package
diff for reference:
diff --git a/node_modules/graphql-modules/package.json b/node_modules/graphql-modules/package.json
index 00c64ab..c5b45b2 100644
--- a/node_modules/graphql-modules/package.json
+++ b/node_modules/graphql-modules/package.json
@@ -30,11 +30,13 @@
"exports": {
".": {
"require": "./index.js",
- "import": "./index.mjs"
+ "import": "./index.mjs",
+ "types": "./index.d.ts"
},
"./*": {
"require": "./*.js",
- "import": "./*.mjs"
+ "import": "./*.mjs",
+ "types": "./*.d.ts"
},
"./package.json": "./package.json"
}
@patrickjm could you please take a look at this. It seems you fixed types in source code in this PR #2324 but the final package.json
doesn't have that fix
Any updates on this @patrickjm ? These are the exports from the published npmjs package, they seem to miss the types
:
No updates here, would recommend making another PR for this one.
edit: I am not a maintainer of this repo
No updates here, would recommend making another PR for this one.
The source code looks fine, this seems to be connected to the publishing pipeline.
@patrickjm Any update?
Does it transitively depend on your action?
graphql-modules/.github/workflows/release.yml
Lines 7 to 9 in 0c72ee4
Do you know why the "types"
field is missing while in the release pipeline?
("exports"
does not exist in package.json
in the repo, but seems to be generated while publishing.)