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

Svelte 5: Binding on Generic Props shows error #2494

Open
nadanke opened this issue Sep 10, 2024 · 1 comment
Open

Svelte 5: Binding on Generic Props shows error #2494

nadanke opened this issue Sep 10, 2024 · 1 comment
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.

Comments

@nadanke
Copy link

nadanke commented Sep 10, 2024

Describe the bug

I have a component that uses generics and takes a bindable prop that looks like this:

{
	current: T;
	default: T;
	error: string | null;
}

This worked fine until I upgraded, now it shows errors in both Neovim and VSCode and also in svelte-check. It still works the same but every time the component is used it shows an error: Type '{ current: unknown; default: unknown; error: string | null; }' is not assignable to type '{ current: number; default: number; error: null; }'. Types of property 'current' are incompatible. Type 'unknown' is not assignable to type 'number'.

svelte-check didn't show the error until i ran this upgrade:

devDependencies:
- svelte-check 3.8.6
+ svelte-check 4.0.1
- typescript-eslint 8.3.0
+ typescript-eslint 8.5.0

but the error still shows up in Neovim/Code even before the upgrade, so I don't think the problem is in svelte-check itself, which is why I'm creating the issue here.

Reproduction

I have not been able to reproduce this in the REPL but it's not a lot of code.
The component:

<script lang="ts" generics="T">
	let {
		input = $bindable()
	}: {
		input: {
			current: T;
			default: T;
			error: string | null;
		};
	} = $props();
</script>

then use normally

<script lang="ts">
	import MyComp from '$lib/MyComp.svelte';

	let x = $state({ current: 1, default: 2, error: null });
</script>

<MyComp bind:input={x} />

It works if you don't bind it, or if you pass in {x as typeof x}, which is weird

Logs

No response

System Info

System:
    OS: Linux 6.10 Arch Linux
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i5-1240P
    Memory: 22.74 GB / 31.06 GB
    Container: Yes
    Shell: 5.2.32 - /bin/bash
  Binaries:
    Node: 22.8.0 - /usr/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 10.2.4 - ~/.local/share/pnpm/npm
    pnpm: 9.1.1 - ~/.local/share/pnpm/pnpm
    bun: 1.1.4 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 128.1.69.162
  npmPackages:
    svelte: 5.0.0-next.244 => 5.0.0-next.244

Severity

annoyance

@dummdidumm dummdidumm transferred this issue from sveltejs/svelte Sep 10, 2024
@dummdidumm dummdidumm added the bug Something isn't working label Sep 10, 2024
@dummdidumm
Copy link
Member

Result of #2477 - we silence type errors due to the whole type being unknown, but not if one of its members is unknown.

@dummdidumm dummdidumm added the Fixed Fixed in master branch. Pending production release. label Sep 19, 2024
dummdidumm added a commit that referenced this issue Sep 19, 2024
This reverts commit 8c080cf.

This reverts #2477. Sadly, the idea didn't work out, as shown by two opened bug reports:

- #2506: A type union can be narrowed on the input, but not on the way back out
- #2494: A generic nested within a bound value is not properly resolved and not falling back to `any` (in #2477 we thought of the generic case, but only for when the generic is the whole value type, not when it's nested)

For these reasons I don't see a way to properly implement #1392 at the moment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.
Projects
None yet
Development

No branches or pull requests

2 participants