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

Type shadowing of global packages due to baseUrl #21070

Closed
IlyaSemenov opened this issue May 26, 2023 · 1 comment · Fixed by #21081
Closed

Type shadowing of global packages due to baseUrl #21070

IlyaSemenov opened this issue May 26, 2023 · 1 comment · Fixed by #21081

Comments

@IlyaSemenov
Copy link

IlyaSemenov commented May 26, 2023

Environment

  • Operating System: Linux
  • Node Version: v16.14.2
  • Nuxt Version: 3.5.1
  • Nitro Version: 2.4.1
  • Package Manager: npm@7.17.0
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/nuxt-starter-gjecyr?file=app.vue,graphql%2Findex.ts

Run npm run dev, you will see graphql version is displayed by app.vue, meaning code imports from the global graphql package.

Run nuxi typecheck, you will see that it doesn't type check, as types import from the local graphql directory:

❯ nuxi typecheck
Nuxi 3.5.1
app.vue:2:8 - error TS1192: Module '"/home/projects/nuxt-starter-gjecyr/graphql/index"' has no default export.

2 import graphql from 'graphql';
         ~~~~~~~


Found 1 error in app.vue:2

Describe the bug

Adding a directory under project root shadows the global package with the same name when type checking (not in runtime).

In the reproduction, I added graphql/index.ts under Nuxt project root. That breaks import ... from "graphql" for type checking. What is worse, type imports break inside other libraries such as @urql/vue or villus which is hard to diagnose.

Additional context

This is caused by .nuxt/tsconfig.json having baseUrl pointing to project root. I believe that serves no real purpose and is a time bomb for types shadowing. As I gathered, it's only needed for setting up non-relative path aliases such as "#imports": [".nuxt/imports"] which is simple to avoid (the majority of the generated aliases are already relative).

I tried removing baseUrl and converting remaining non-relative path aliases to relative ("@/*": ["../*"], "#imports": ["./imports"], etc.) and everything seemed to work fine.

Logs

No response

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

Successfully merging a pull request may close this issue.

3 participants