nuxt / nuxt/module-builder

error TS2742: The inferred type of 'default' cannot be named without a reference

Open
#141 22 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug workaround available
Dominant language
TypeScript
Stars
275
Forks
35
Avg merge
4h 42m
Merged PRs (30d)
6

Description

Im trying to pnpm prepack a nuxt3 module with a package which exports a simple object and its types file.

1 - In the playground/node_modules/myPackage/dist

index.d.ts :

type Params = string[];

declare const endpoints: {
    abc: (q: Params) => string;
};

export { Params, endpoints as default };

index.js :

// src/index.ts
var endpoints = {
  abc: (q) => "test"
};
var src_default = endpoints;
export {
  src_default as default
};

2 - In the playground/app.config.ts

import endpoints from "myPackage";

export default {
    endpoints
};

3 - In the module, src/runtime/plugin.ts :

import { defineNuxtPlugin, useAppConfig } from "#imports";

export default defineNuxtPlugin(() => {
   
  return {
    provide: {
      backend: useAppConfig().endpoints, 
    },
  };
});

Then, i run pnpm prepack and i always have this error :

ℹ Building my-module                                                                                                                                                                                                                                  12:16:16 PM
src/runtime/plugin.ts(3,1): error TS2742: The inferred type of 'default' cannot be named without a reference to '../../playground/node_modules/@aurionsarl/auberdog-pension-api/dist'. This is likely not portable. A type annotation is necessary.
src/runtime/plugin.ts(3,1): error TS2742: The inferred type of 'default' cannot be named without a reference to '../../playground/node_modules/@aurionsarl/auberdog-pension-api/dist'. This is likely not portable. A type annotation is necessary.

Error [RollupError]: Failed to compile. Check the logs above.
    at error (file:///home/tony/projects/testmodule/node_modules/.pnpm/rollup@3.26.2/node_modules/rollup/dist/es/shared/node-entry.js:2245:30)
    at Object.error (file:///home/tony/projects/testmodule/node_modules/.pnpm/rollup@3.26.2/node_modules/rollup/dist/es/shared/node-entry.js:25139:20)
    at Object.error (file:///home/tony/projects/testmodule/node_modules/.pnpm/rollup@3.26.2/node_modules/rollup/dist/es/shared/node-entry.js:24262:42)
    at generateDtsFromTs (file:///home/tony/projects/testmodule/node_modules/.pnpm/rollup-plugin-dts@5.3.0_rollup@3.26.2_typescript@5.1.6/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1697:30)
    at Object.transform (file:///home/tony/projects/testmodule/node_modules/.pnpm/rollup-plugin-dts@5.3.0_rollup@3.26.2_typescript@5.1.6/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1706:38)
    at file:///home/tony/projects/testmodule/node_modules/.pnpm/rollup@3.26.2/node_modules/rollup/dist/es/shared/node-entry.js:25332:40
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  id: '/home/tony/projects/testmodule/src/module.ts',
  hook: 'resolveId',
  code: 'PLUGIN_ERROR',
  plugin: 'commonjs--resolver',
  watchFiles: [
    '/home/tony/projects/testmodule/src/module.ts',
    '/home/tony/projects/testmodule/src/runtime/plugin.ts'
  ]
}
 ELIFECYCLE  Command failed with exit code 1.

And the error disappears if i replace the code in the playground/node_modules/myPackage/dist by this one :

type Params = string; // replace string[] to string

declare const endpoints: {
    abc: (q: Params) => string;
};

export { Params, endpoints as default };

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/runtime/plugin.ts and the playground/app.config.ts reproduction, then inspect the generated declarations under playground/node_modules/myPackage/dist. Run pnpm prepack to reproduce TS2742 and compare the failing declaration with the variant using Params = string; done means the module builds without the non-portable inferred-type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rollup, typescript
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.