Skip to content

TypeScript issues with moduleResolution: "nodenext" and declaration: true #18

@eugene1g

Description

@eugene1g

Guidelines

  • Please search other issues to make sure this bug has not already been reported.

Describe the bug

When using TypeScript with "moduleResolution: "nodenext" and "declaration": true", it fails to compile the example from README

Steps to reproduce

main.ts

import ModernError from 'modern-errors'
export const BaseError = ModernError.subclass('BaseError')

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "nodenext",
    "declaration": true
  }
}

error:

main.ts:3:14 - error TS2742: The inferred type of 'BaseError' cannot be named without a reference to './node_modules/modern-errors/build/src/subclass/create.js'. This is likely not portable. A type annotation is necessary.

3 export const BaseError = ModernError.subclass('BaseError')
               ~~~~~~~~~

main.ts:3:14 - error TS2742: The inferred type of 'BaseError' cannot be named without a reference to './node_modules/modern-errors/build/src/subclass/custom.js'. This is likely not portable. A type annotation is necessary.

3 export const BaseError = ModernError.subclass('BaseError')
               ~~~~~~~~~


Found 2 errors in the same file, starting at: main.ts:3

I can make it work in two ways -

  1. Disable declaration. However, this means my library cannot publish types and that's not great.
  2. Change to "moduleResolution": "node". However, this means I cannot use the exports settings in my dependencies.

With "moduleResolution":"node" it generates these types for main.d.ts:

export declare const BaseError: import("modern-errors/build/src/subclass/create").ErrorSubclassCore<[], {}, import("modern-errors/build/src/subclass/custom").CustomClass>;

Which hints at the reason for failure: TypeScript has to tap into internal files to reference un-exported types to get ErrorSubclassCore (and, in my real project, CustomClass). With "moduleResolution":"node", this deep linking is not possible, so the TS compiler gives up.

Environment

  System:
    OS: macOS 13.1
    CPU: (8) arm64 Apple M1
    Memory: 64.61 MB / 8.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.14.0 - ~/.asdf/installs/nodejs/18.14.0/bin/node
    Yarn: 1.22.19 - ~/.asdf/shims/yarn
    npm: 9.3.1 - ~/.asdf/plugins/nodejs/shims/npm

Pull request (optional)

  • I can submit a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions