module-builder icon indicating copy to clipboard operation
module-builder copied to clipboard

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

Open Aurion72 opened this issue 11 months ago • 4 comments

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/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:2245:30)
    at Object.error (file:///home/tony/projects/testmodule/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:25139:20)
    at Object.error (file:///home/tony/projects/testmodule/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:24262:42)
    at generateDtsFromTs (file:///home/tony/projects/testmodule/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1697:30)
    at Object.transform (file:///home/tony/projects/testmodule/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1706:38)
    at file:///home/tony/projects/testmodule/node_modules/.pnpm/[email protected]/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 };

Aurion72 avatar Jul 13 '23 10:07 Aurion72

@danielroe It seems to be related to this ticket https://github.com/egoist/tsup/issues/624

Aurion72 avatar Jul 24 '23 08:07 Aurion72

Running prepack on my module was fine on 3.6.3, but breaks on 3.6.4 with this same type of error. Same code, just bumped the deps.

agenordebriat avatar Jul 27 '23 13:07 agenordebriat

Facing the same issue with custom interfaces

RomanSkrypnik avatar Apr 22 '24 16:04 RomanSkrypnik

I think there is a way we can solve this within @nuxt/module-builder but until then you can resolve (with the latest version - and make sure you have mkdist@^1.5.1) by importing from the library which can't be named.

Something like this would likely work...

+ import type {} from 'myPackage'
  import { defineNuxtPlugin, useAppConfig } from "#imports";
  
  export default defineNuxtPlugin(() => {
     
    return {
      provide: {
        backend: useAppConfig().endpoints, 
      },
    };
  });

See https://github.com/microsoft/TypeScript/pull/58176#issuecomment-2052698294 for more about why this is happening.

danielroe avatar Apr 25 '24 22:04 danielroe