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

Problem with kit.alias for $types #12169

Open
teneon opened this issue Apr 26, 2024 · 0 comments
Open

Problem with kit.alias for $types #12169

teneon opened this issue Apr 26, 2024 · 0 comments

Comments

@teneon
Copy link

teneon commented Apr 26, 2024

Describe the bug

When attempting to use kit.alias to set aliases for TypeScript types in the .svelte-kit/types/src/routes directory, TypeScript fails to recognize these aliases unless baseUrl is explicitly set in tsconfig.json. However, setting baseUrl directly conflicts with SvelteKit's built-in behavior of generating tsconfig.json, leading to a warning against such modifications. This creates a challenging scenario where users are unable to utilize custom aliases for auto-generated types, impacting development workflow and code organization.

Reproduction

  1. Initialize a standard SvelteKit project with TypeScript enabled.
  2. Modify svelte.config.js and add an alias to the kit configuration as follows:
kit: {
  alias: {
    $routes: '.svelte-kit/types/src/routes'
  }
}
  1. In my HeaderLeft.svelte component, which is located here: "src/lib/layouts/main/HeaderLeft.svelte"
    By using the added "$routes" alias, the import seems to works properly:
    import type { LayoutData } from '$routes/[[lang=langCode]]/$types';

Without alias i need to import it relative like this and it is cumbersome:
import type { LayoutData } from '../../../routes/[[lang=langCode]]/$types';

  1. The issue is that if i add that alias, i also get VSCode error tsconfig.json: Non-relative paths are not allowed when 'baseUrl' is not set. Did you forget a leading './'?

  2. If i add baseUrl to the tsconfig.json i get a warning when i do "npm run dev" i get the following message:
    "You have specified a baseUrl and/or paths in your tsconfig.json which interferes with SvelteKit's auto-generated tsconfig.json. Remove it to avoid problems with intellisense. For path aliases, use kit.alias instead: https://kit.svelte.dev/docs/configuration#alias"

So this surely isn't the right way.

  1. I have also tried different alias paths to see if it might work, but it didn't work.
    $routes: './svelte-kit/types/src/routes'
    $routes: './.svelte-kit/types/src/routes'
    and so on...
    Either the import via $route didn't resolve anymore or if "." is added in front then i get the other error message: "Non-relative paths are not allowed when 'baseUrl' is not set."

I am not sure if this is a bug / a missing feature or if i don't understand something and i am doing it totally wrong. Please help! Thank you in advance! :)

best regards,
Jim

Logs

npm run dev -- --host

> project@0.0.1 dev
> vite dev --host

You have specified a baseUrl and/or paths in your tsconfig.json which interferes with SvelteKit's auto-generated tsconfig.json. Remove it to avoid problems with intellisense. For path aliases, use `kit.alias` instead: https://kit.svelte.dev/docs/configuration#alias

  VITE v5.2.8  ready in 984 ms

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (20) x64 Intel(R) Core(TM) i9-10900K CPU @ 3.70GHz
    Memory: 103.54 GB / 125.69 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.12.1 - ~/.nvm/versions/node/v20.12.1/bin/node
    npm: 10.5.0 - ~/.nvm/versions/node/v20.12.1/bin/npm
  Browsers:
    Chrome: 123.0.6312.86
  npmPackages:
    @sveltejs/adapter-auto: ^3.2.0 => 3.2.0 
    @sveltejs/kit: ^2.5.5 => 2.5.5 
    svelte: ^4.2.12 => 4.2.12 
    vite: ^5.2.8 => 5.2.8

Severity

annoyance

Additional Information

No response

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

1 participant