language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

d.ts generation failed because of `$$__sveltets_2_IsomorphicComponent` naming conflict

Open targetlucked69 opened this issue 1 year ago • 8 comments

Describe the bug

Hello, is this not possible in Svelte? Seems like the compiler is having a meltdown

import _Button from './Button.svelte';
import ButtonLink from './ButtonLink.svelte';

const Button = Object.assign(_Button, {
    Link: ButtonLink,
});

export { UserButton };

Im getting this d.ts error when building in library mode

> [email protected] package
> svelte-kit sync && svelte-package && publint

d.ts type declaration files for the following files were likely not generated due to the following errors:
/tmp/my-lib/src/lib/components/Button/index.ts
  - Exported variable 'Button' has or is using name '$$__sveltets_2_IsomorphicComponent' from external module "/tmp/my-lib/src/lib/components/Button/Button.svelte" but cannot be named.
  - Exported variable 'Button' has or is using name '$$__sveltets_2_IsomorphicComponent' from external module "/tmp/my-lib/src/lib/components/Button/ButtonLink.svelte" but cannot be named
src/lib -> dist
@sveltejs/package found the following issues while packaging your library:
You are using SvelteKit-specific imports in your code, but you have not declared a dependency on `@sveltejs/kit` in your `package.json`. Add it to your `dependencies` or `peerDependencies`.

Sample usage in Svelte:

<script>
import { Button } from 'my-lib'
</script>

<Button>
    <Button.Link href="/somewhere" />
</Button>

Reproduction

  1. Create a SvelteKit app in library mode
  2. Re-export a component like:
import Button from './Button.svelte';

export const RenamedButton = Button;
  1. Run npm run build and see the console output error about d.ts

Logs

No response

System Info

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 110.84 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.17.0 - ~/.local/state/fnm_multishells/40146_1728685539617/bin/node
    npm: 10.8.2 - ~/.local/state/fnm_multishells/40146_1728685539617/bin/npm
    pnpm: 9.12.0 - /opt/homebrew/bin/pnpm
    bun: 1.1.30 - ~/.bun/bin/bun
  Browsers:
    Chrome: 129.0.6668.100
    Safari: 17.5
  npmPackages:
    svelte: ^5.0.0-next.262 => 5.0.0-next.262

Severity

annoyance

targetlucked69 avatar Oct 12 '24 23:10 targetlucked69

Can you provide the code for button and buttonlink?

dummdidumm avatar Oct 13 '24 16:10 dummdidumm

Describe the bug

Hello, is this not possible in Svelte? Seems like the compiler is having a meltdown

import _Button from './Button.svelte';
import ButtonLink from './ButtonLink.svelte';

const Button = Object.assign(_Button, {
    Link: ButtonLink,
});

export { UserButton };

Im getting this d.ts error when building in library mode

> [email protected] package
> svelte-kit sync && svelte-package && publint

d.ts type declaration files for the following files were likely not generated due to the following errors:
/tmp/my-lib/src/lib/components/Button/index.ts
  - Exported variable 'Button' has or is using name '$$__sveltets_2_IsomorphicComponent' from external module "/tmp/my-lib/src/lib/components/Button/Button.svelte" but cannot be named.
  - Exported variable 'Button' has or is using name '$$__sveltets_2_IsomorphicComponent' from external module "/tmp/my-lib/src/lib/components/Button/ButtonLink.svelte" but cannot be named
src/lib -> dist
@sveltejs/package found the following issues while packaging your library:
You are using SvelteKit-specific imports in your code, but you have not declared a dependency on `@sveltejs/kit` in your `package.json`. Add it to your `dependencies` or `peerDependencies`.

Sample usage in Svelte:

<script>
import { Button } from 'my-lib'
</script>

<Button>
    <Button.Link href="/somewhere" />
</Button>

Reproduction

  1. Create a SvelteKit app in library mode
  2. Re-export a component like:
import Button from './Button.svelte';

export const RenamedButton = Button;
  1. Run npm run build and see the console output error about d.ts

Logs

No response

System Info

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 110.84 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.17.0 - ~/.local/state/fnm_multishells/40146_1728685539617/bin/node
    npm: 10.8.2 - ~/.local/state/fnm_multishells/40146_1728685539617/bin/npm
    pnpm: 9.12.0 - /opt/homebrew/bin/pnpm
    bun: 1.1.30 - ~/.bun/bin/bun
  Browsers:
    Chrome: 129.0.6668.100
    Safari: 17.5
  npmPackages:
    svelte: ^5.0.0-next.262 => 5.0.0-next.262

Severity

annoyance

Rock1raw avatar Oct 31 '24 15:10 Rock1raw

Can you explain to me what's going on?

Rock1raw avatar Nov 02 '24 20:11 Rock1raw

Hello

Rock1raw avatar Nov 03 '24 22:11 Rock1raw

I've also run into this issue. The way I got it to shut up and work was to update your repro to the following:

import Button from './Button.svelte';

export const RenamedButton = Button as typeof Button

huntabyte avatar Nov 15 '24 20:11 huntabyte

This is a general TypeScript limitation, for some reason it refuses to use typeof Thing and instead tries to replicate the type from the imported component. That does only work if all ingredients for that are exported. In this case it's $$__sveltets_2_IsomorphicComponent that's missing, but you could just as well create this error yourself if you would use an interface that is not exported.

The workaround that @huntabyte shows is the correct fix to solve this. Alternatively you can also do

import Button from './Button.svelte';

export const RenamedButton: typeof Button = Button

This forces TS to say "oh alright I'm just gonna use typeof TheImport as the type", which it could also do in the other case but for some reason does not.

dummdidumm avatar Nov 16 '24 18:11 dummdidumm

What are you do?

Rock1raw avatar Nov 21 '24 08:11 Rock1raw

This is a general TypeScript limitation, for some reason it refuses to use typeof Thing and instead tries to replicate the type from the imported component. That does only work if all ingredients for that are exported. In this case it's $$__sveltets_2_IsomorphicComponent that's missing, but you could just as well create this error yourself if you would use an interface that is not exported.

The workaround that @huntabyte shows is the correct fix to solve this. Alternatively you can also do


import Button from './Button.svelte';



export const RenamedButton: typeof Button = Button

This forces TS to say "oh alright I'm just gonna use typeof TheImport as the type", which it could also do in the other case but for some reason does not.

Rock1raw avatar Nov 21 '24 08:11 Rock1raw