Skip to content

highlight.js types don't get loaded with TypeScript's nodenext resolution #3481

@DanielRosenwasser

Description

@DanielRosenwasser

Recently, TypeScript has added a new resolution mode for Node ESM files. This mode is enabled when running with --moduleResolution nodenext. Under this mode, ESM files respect rules like the exports field on packages. Any .mts or .mjs file is an ES module, and any .ts and .js file is ESM if a containing package.json contains the field "type": "module".

When trying to consume highlight.js under this mode, it currently doesn't pick up on the types being shipped. I can't seem to find the package.json contents that have been shipped on npm (https://unpkg.com/browse/[email protected]/package.json) but this is because each exports field needs to have its own types field as well; otherwise, TypeScript will prefer to look for a corresponding .d.ts file wherever the require or import fields point to.

I believe that the correct solution is to provide the following fields in your package.json.

    ".": {
      "types": "./types/index.d.ts",
      "require": "./lib/index.js",
      "import": "./es/index.js"
    },
    "./*" {
      "types": "./types/index.d.ts"
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions