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

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

Open
TonyFresneau opened this issue Jul 13, 2023 · 17 comments
Labels
bug Something isn't working workaround available

Comments

@TonyFresneau
Copy link

TonyFresneau commented Jul 13, 2023

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 };
@TonyFresneau
Copy link
Author

@danielroe It seems to be related to this ticket egoist/tsup#624

@agenordebriat
Copy link

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.

@RomanSkrypnik
Copy link

Facing the same issue with custom interfaces

@danielroe
Copy link
Member

danielroe commented Apr 25, 2024

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 microsoft/TypeScript#58176 (comment) for more about why this is happening.

@sandros94

This comment has been minimized.

@ijkml

This comment has been minimized.

@danielroe

This comment has been minimized.

@ijkml

This comment has been minimized.

@danielroe

This comment has been minimized.

@sandros94

This comment has been minimized.

@dnldsht
Copy link

dnldsht commented Dec 13, 2024

I'm having the same issue with a custom useFetch

import { useFetch, useNuxtApp, type UseFetchOptions } from '#app'
import type {} from 'ofetch'
import type {} from 'nuxt/app' // doesn't work

type Wrapper<T> = { data: T }

export function useApiFetch<T>(url: string | (() => string), options: UseFetchOptions<T> = {}) {
  return useFetch<Wrapper<T>>(url, {
    $fetch: useNuxtApp().$fetch as typeof $fetch,
    transform: res => res.data as T,
    lazy: true,
    ...options,
  })
}

When i run prepack i get the following error:

error TS2742: The inferred type of 'useApiFetch' cannot be named without a reference to '~/node_modules/nuxt/dist/app/composables/asyncData'. This is likely not portable. A type annotation is necessary.

By adding import type {} from 'ofetch' i managed to remove this error

error TS2742: The inferred type of 'useApiFetch' cannot be named without a reference to '~/node_modules/ofetch/dist'. This is likely not portable. A type annotation is necessary.

I don't understand what kind of type should import

@JustusBaumhove
Copy link

@dnldsht you can probably get away with the approach in this comment here, that uses typeof useFetch to infer prop and return types.

I am facing a similar issue tho trying to make a composable that extends opts and returns.

@alvarosabu
Copy link

I am facing the same issue on https://github.com/Tresjs/nuxt after bumping the deps.

> nuxt-module-build prepare && nuxt-module-build build

✔ Types generated in .nuxt                                                                                                                                          10:10:19 AM
ℹ Building nuxt                                                                                                                                                     10:10:20 AM
ℹ Cleaning dist directory: ./dist                                                                                                                                   10:10:20 AM
.nuxt/components.plugin.mjs(3,1): error TS2742: The inferred type of 'default' cannot be named without a reference to '~/node_modules/nuxt/dist/app/nuxt.js'. This is likely not portable. A type annotation is necessary.


 ERROR  Failed to compile. Check the logs above.                                                                                      

@danielroe
Copy link
Member

@alvarosabu The issue you are facing is superficially similar but in fact vuejs/language-tools#5018. You can downgrade/pin your typescript version to ~5.6 to resolve.

@alvarosabu
Copy link

alvarosabu commented Dec 19, 2024

Thanks, @danielroe, I confirm downgrading to "typescript": "^5.6.3", did solve the issue with building the module.

volkish added a commit to volkish/yandex-metrika-nuxt that referenced this issue Dec 24, 2024
IlyaSemenov added a commit to kingyue737/nuxt-vite-legacy that referenced this issue Dec 26, 2024
IlyaSemenov added a commit to IlyaSemenov/nuxt-vite-legacy that referenced this issue Dec 26, 2024
* Support Nuxt 3.15, @vitejs/plugin-legacy 6.0

* Add changeset

* Downgrade typescript, fix module build

See nuxt/module-builder#141

---------

Co-authored-by: Ilya Semenov <[email protected]>
acidjazz added a commit to fumeapp/care that referenced this issue Jan 20, 2025
@acidjazz
Copy link
Collaborator

Do we know if this was fixed? do we need to bump the deps in this repo to close this?

Copy link
Member

This should be resolved in most cases with v1 of module-builder - there are still a few pending PRs in mkdist to resolve before releasing it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working workaround available
Projects
None yet
Development

No branches or pull requests

10 participants