-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
Typescript > Slot props are treated as const unless the prop is exported #2116
Comments
From TypeScript's perspective this is correct because nothing is reassigning the slot prop so TS will narrow it to the value you have given it. The problem is, even if you then modify the prop somewhere, TS won't recognize that and widen the type. We work around this in the language server by adding code that forces the type to widen, but we only do that for things we know are reactive, like props or reactive statements. This one's different as it could become reactive if it's mutated, at which point we need to apply the same behavior. I believe Svelte can give us the info which of the things are reactive, so we maybe need to use that info to then apply the force-widening transformation. |
I'm getting this with a fresh sveltekit install with a reactive variable. It's literally a straight install from https://kit.svelte.dev/ as at 2024/03/12 with this svelte example file put in <script lang="ts">
let problem: 'Foo' | 'Bar' = 'Foo';
</script>
{#if problem === 'Bar'}
It's absolutely got an overlap
{/if}
<select bind:value={problem}>
<option value="Foo">Foo</option>
<option value="Bar">Bar</option>
</select> > check-test@0.0.1 check
> svelte-kit sync && svelte-check --tsconfig ./tsconfig.json
====================================
Loading svelte-check in workspace: /<redact>/Development/Projects/check-test
Getting Svelte diagnostics...
/<redact>/Development/Projects/check-test/src/routes/check.svelte:5:6
Error: This comparison appears to be unintentional because the types '"Foo"' and '"Bar"' have no overlap. (ts)
{#if problem === 'Bar'}
It's absolutely got an overlap
====================================
svelte-check found 1 error and 0 warnings in 1 file These are my deps
npm 10.2.5 |
This sounds related to TypeScript 5.4 - #2313 will add support for it. |
Closing as won't fix - snippets will replace slots and don't have this problem |
Describe the bug
TLDR; take a look at the reproduction.
Slot props are treated as const unless the variable is exported.
https://www.loom.com/share/1d8ff3fbd9fc476e8db086f5abcb948c - video
Reproduction
https://github.com/oodavid/svelte-slot-props/tree/main/src/routes
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: