Skip to content

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

@basicdays

Description

@basicdays

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions