svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Identifier has alreay been declared error when naming a type the same as a variable in svelte5 script

Open zhihengGet opened this issue 1 year ago • 3 comments

Describe the bug

cant have smae name as variable ...

after using @sveltejs/[email protected]

src/routes/+page.svelte:4:12 Identifier 'data' has already been declared
src/routes/+page.svelte:4:12
2 |    import Counter from '$lib/Counter.svelte';
 3 |    type data = {}
 4 |    export let data;
                   ^
 5 |  </script>

Reproduction URL

<script lang="ts">
	import Counter from '$lib/Counter.svelte';
	type data = {}
	export let data;
</script>

Hello {data.name}!

Reproduction

link

Logs

-

System Info

-

zhihengGet avatar Sep 04 '24 03:09 zhihengGet

moving to svelte repo

svelte5 repl

dominikg avatar Sep 04 '24 08:09 dominikg

adding info from @gtm-nayan

cause is in acorn-typescript that just has one scope for both https://github.com/TyrealHu/acorn-typescript/blob/8956dc50370c7ee2ea97a3c903611079192b28d5/src/index.ts#L2720 https://github.com/acornjs/acorn/blob/12ad1164a70dbe804f1ece822f34d100545f1afa/acorn/src/lval.js#L267

dominikg avatar Sep 04 '24 11:09 dominikg

In the meantime, either name them differently, or reenable the script preprocessor in your config (it was disabled by default for vite-plugin-svelte 4): preprocess: vitePreprocess({ script: true })

dummdidumm avatar Sep 04 '24 11:09 dummdidumm

reenable the script preprocessor in your config (it was disabled by default for vite-plugin-svelte 4): preprocess: vitePreprocess({ script: true })

@dummdidumm I tried this and it works, but is there a way to get rid of the red squiggly lines

Image

levibassey avatar Jan 22 '25 12:01 levibassey