Skip to content

False positive TypeScript error when spreading attributes on div HTML elementΒ #13959

@kwangure

Description

@kwangure

Describe the bug

There's a type mismatch between the <div> element type and HTMLAttributes<HTMLDivElement>

Reproduction

Type this into a Svelte file in VS Code. Notice the error on the spread of restProps on the <div>.

<script lang="ts">
	import type { HTMLAttributes } from 'svelte/elements';

	const { children, ...restProps }: HTMLAttributes<HTMLDivElement> = $props();
</script>

<div {...restProps}>
	{@render children?.()}
</div>

I've encountered a few issues related to spreading of props. Most are fairly simple to work around by just Omit<>ing the offending attribute and typing it correctly. As a catch all, it might be a good idea to create a TypeScript test for all of the elements in import('svelte/element').SvelteHTMLElements.

Logs

Types of property 'hidden' are incompatible.
      Type 'boolean | "" | "until-found" | null | undefined' is not assignable to type 'boolean | null | undefined'.
        Type '""' is not assignable to type 'boolean | null | undefined'.ts(2345)

System Info

Svelte: ^5.1.3 => 5.1.3

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions