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

Types aren't correctly exported for CJS #2047

Open
2 tasks done
Oupsla opened this issue Dec 11, 2024 · 2 comments
Open
2 tasks done

Types aren't correctly exported for CJS #2047

Oupsla opened this issue Dec 11, 2024 · 2 comments
Assignees
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@Oupsla
Copy link

Oupsla commented Dec 11, 2024

Description

Typescript with "moduleResolution": "node16" will generate an error when trying to import the module from a cjs file.

Could not find a declaration file for module 'openapi-typescript'. '/node_modules/openapi-typescript/dist/index.cjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/openapi-typescript` if it exists or add a new declaration (.d.ts) file containing `declare module 'openapi-typescript';`ts(7016)
Name Version
openapi-typescript 7.4.4
Node.js 22.9.0

Reproduction

Check here : https://arethetypeswrong.github.io/?p=openapi-typescript%407.4.4

Checklist

@Oupsla Oupsla added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Dec 11, 2024
@Oupsla
Copy link
Author

Oupsla commented Dec 11, 2024

I think the package.json of each module should be something like :

"exports": {
    "./package.json": "./package.json",
    ".": {
      "import": {
        "types": "./dist/index.d.ts",
        "default": "./dist/index.js"
      },
      "require": {
        "types": "./dist/index.d.ts",
        "default": "./dist/index.cjs"
      }
    }
  },

@wattry
Copy link

wattry commented Dec 19, 2024

I think the package.json of each module should be something like :

"exports": {
    "./package.json": "./package.json",
    ".": {
      "import": {
        "types": "./dist/index.d.ts",
        "default": "./dist/index.js"
      },
      "require": {
        "types": "./dist/index.d.ts",
        "default": "./dist/index.cjs"
      }
    }
  },

This depends on what the maintainers are attempting to achieve though the TS config is incorrect. The post above is mostly correct though there's no need to define the same types for require vs export as that will be handled by TypeScript.

This "./*": "./*" is allowing the package user to access any file within the project in a import/require and is likely intentional

  "type": "module",
  "types": "./dist/index.d.ts",
  "module": "./dist/index.js",
  "main": "./dist/index.cjs",
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "import": "./dist/index.js",
      "require": "./dist/index.cjs"
    },
    "./*": "./*"
  }

I tested this locally and was able to import TS, ESM and CJS.

Note

@drwpow drwpow self-assigned this Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

3 participants