Skip to content

Commit

Permalink
fix: relax component constructor type (#2524)
Browse files Browse the repository at this point in the history
Since , we're no longer adding the `[evt: string]: CustomEvent<any>` index signature to components in runes mode not using `createEventDispatcher`. This revealed a type bug in our `ATypedSvelteComponent` type. It was to restricting, because TypeScript will resolve the empty event object to a handler with callback type `never`, which then means the `__sveltets_2_ensureComponent` generic no longer picks the right type, resulting in generics not being resolved properly. Fix this by relaxing the constraints.

Fixes #2523
  • Loading branch information
dummdidumm authored Sep 27, 2024
1 parent fc2144b commit 4dfb988
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script lang="ts" generics="T">
let { value, defaultValue }: { value: T; defaultValue?: T } = $props();
</script>

{value}
{defaultValue}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"code": 2322,
"message": "Type 'number' is not assignable to type 'string'.",
"range": {
"end": {
"character": 36,
"line": 10
},
"start": {
"character": 24,
"line": 10
}
},
"severity": 1,
"source": "ts",
"tags": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script lang="ts">
import ValueComponent from './ValueComponent.svelte';
let value = $state("test");
</script>

<!-- ok -->
<ValueComponent {value} defaultValue={"foo"} />

<!-- error -->
<ValueComponent {value} defaultValue={1} />
2 changes: 1 addition & 1 deletion packages/svelte2tsx/svelte-shims-v4.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ declare type ATypedSvelteComponent = {
*/
$$slot_def: any;

$on(event: string, handler: ((e: any) => any) | null | undefined): () => void;
$on(event: string, handler: any): () => void;
}
/**
* Ambient type only used for intellisense, DO NOT USE IN YOUR PROJECT.
Expand Down

0 comments on commit 4dfb988

Please sign in to comment.