Skip to content

Unresolved TypeScript definition paths (when using node-esm with imports) #48639

Closed
@mannie-exe

Description

@mannie-exe

Hey there!

Bug Report

I'm writing a small, shared module to be imported in other TypeScript programs and I noticed a slight error with my generated TypeScript definitions (that might not be a bug, yet I'm unsure of how to fix).

I wanted to be able to reference my src folder in my project root as #src, so I added the required imports and paths in my package.json and tsconfig.json respectively.

After letting tsc generate my JavaScript, I'm able to run the project just fine -- however, importing my library in another TS project showed me that the TypeScript definitions were also using the Node.js-like #src path, but were obviously not resolving properly and couldn't be loaded.

What would be my best course of action to fix this? I understand that this type of module resolution is just something Node.js's doing (based off of packge.json/imports), so what would TypeScript's solution be to projects using this feature? Can I use custom, local imports in my package.json yet resolve emitted type definitions correctly?

🔎 Search Terms

package.json esm imports type declarations emitted path incorrect resolve

🕗 Version & Regression Information

  • This can be a crash if: an ESM-based Node.js package, with imports, generates types that are consumed anywhere
  • This has only been tested on the TypeScript@4.7.0-dev.20220408 nightly build

💻 Code

Here are the relevant bits from my package.json file:

{
  "type": "module",
  "exports": {
    ".": {
      "types": "./dist-types/index.d.ts",
      "import": "./dist-esm/index.js"
    }
  },
  "imports": {
    "#src/*": [
      "./dist-esm/*.js"
    ]
  },
  "devDependencies": {
    "typescript": "^4.7.0-dev.20220408"
  }
}

...and tsconfig.json file:

{
  "compilerOptions": {
    "lib": [
      "es2022"
    ],
    "target": "es2022",
    "module": "es2022",
    "moduleResolution": "node"
    "esModuleInterop": true,
    "baseUrl": ".",
    "paths": {
      "#src/*": [
        "./src/*.ts"
      ]
    },
    "outDir": "./dist-esm",
    "declarationDir": "./dist-types",
    "declaration": true,
    "declarationMap": true
  }
}

🙁 Actual behavior

The resulting type declarations, ./dist-types/index.d.ts in my project for example, has imports such as:

import { AuthMessageType, LoginRequestMessage, LoginSuccessMessage, LoginFailureMessage } from '#src/messaging/auth';

...and while generated JS in ./dist-esm resolves #src just fine, TypeScript has no idea where to find the rest of my type definitions.

🙂 Expected behavior

I'm not sure to be honest. The TypeScript config's paths definition is technically working just fine, as my source files are mapped correctly -- what would the equivalent, or generally correct way of fixing this for just the type definitions be?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions