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 from *.d.ts files are not parsed in node_modules #929

Open
rvetere opened this issue May 30, 2024 · 1 comment
Open

Types from *.d.ts files are not parsed in node_modules #929

rvetere opened this issue May 30, 2024 · 1 comment

Comments

@rvetere
Copy link
Contributor

rvetere commented May 30, 2024

If an installed package would deliver type informations with d.ts files, it won't parse these files and interface informations are missing in the props.

It could be fixed in the makeFsImporter.ts file with a simple addition:

if (!nextFile) {
      // Customization: try to read from a "d.ts" file instead, if it exists
      if (resolvedSource.includes("node_modules")) {
        const dtsPath = resolvedSource.replace(".js", ".d.ts");
        if (fs.existsSync(dtsPath)) {
          resolvedSource = dtsPath;
        }
      }
      // Customization: end

      // Read and parse the code
      const src = fs.readFileSync(resolvedSource, "utf8");

      nextFile = file.parse(src, resolvedSource);

      parseCache.set(resolvedSource, nextFile);
}

But i didn't want to make another pull request because i'm not sure if this is a nice way to solve it.

@alirezamirian
Copy link

alirezamirian commented Aug 5, 2024

There are currently two issues with types that are imported from .d.ts files:

  1. import resolution: resolve that's used in the built-in fs importer is not built to locate type declaration files. Simply replacing .js with .d.ts is not a reliable solution, as it's quite likely for the type declaration file to be named something else, which is then specified in types or typings key of the package.json file. To make the matters worse, many packages have started to use package.json exports, which can include a types mapping too (example).
  2. Parsing: The parsing doesn't always work for .d.ts files, since it doesn't recognize declare syntax. It's also reported in Can be generated from a difinition file? #814 (comment) and Typescript: Component declaration reference is not recognized by parser #898.

With an increasing number of reusable hooks and headless components, it is becoming common for component libraries to extend prop interfaces exported from third-party packages. Looking forward to improvements in this area, and I could also look into possible solutions for the first point.

Also related: #416, #352, storybookjs/storybook#10034

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants