Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use TS typedefs on an ESM setup #2365

Open
basicdays opened this issue Jun 9, 2023 · 7 comments
Open

Can't use TS typedefs on an ESM setup #2365

basicdays opened this issue Jun 9, 2023 · 7 comments

Comments

@basicdays
Copy link

basicdays commented Jun 9, 2023

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"
      ]
    }
  }
}
@basicdays
Copy link
Author

My current workaround is just to do the following when importing:

import graphqlModules = require("graphql-modules");

const { gql, createModule } = graphqlModules;

@basicdays
Copy link
Author

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"
  }
}

@basicdays
Copy link
Author

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"
   }

@wKich
Copy link

wKich commented Sep 7, 2023

@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

@itpropro
Copy link

itpropro commented Nov 26, 2023

Any updates on this @patrickjm ? These are the exports from the published npmjs package, they seem to miss the types:
image

@patrickjm
Copy link
Contributor

No updates here, would recommend making another PR for this one.

@itpropro
Copy link

itpropro commented Dec 2, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants