language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

type `svelte:self`

Open Zachiah opened this issue 4 years ago • 8 comments

Describe the problem

When you use svelte:self in a typescript component, it seems to not have any type checks

Describe the proposed solution

I would like svelte:self to be typed according to the props in the current component

Alternatives considered

You could always just not use types on svelte:self

Importance

would make my life easier

Zachiah avatar Nov 03 '21 22:11 Zachiah

From a typing perspective that's really tricky because we use the component within its own definition, might very well be that typescript bails out here and we need to find some workaround.

dummdidumm avatar Nov 04 '21 06:11 dummdidumm

i just spent like an hour debugging only to find out i was passing invalid arguments to svelte:self. recursive stuff like this is typically painful to debug so it would be nice if it was typed.

From a typing perspective that's really tricky because we use the component within its own definition, might very well be that typescript bails out here and we need to find some workaround.

typescript is usually pretty good with handling recursive types, and only really complains in cases where it believes the recursion to be infinite.

i have no idea how svelte component types work under the hood though, but assuming it's something like this, there shouldn't be an issue:

declare class SomeComponent {
  constructor(value: number)
  self: typeof SomeComponent // no error
}

new (new SomeComponent(1)).self(1) // no error

DetachHead avatar Nov 24 '22 07:11 DetachHead

On the smae note, I'd be very interested if there were a way to access a component's own types, like SvelteAllProps or typeof $$props

GauBen avatar Feb 24 '23 20:02 GauBen

Is this maybe somehow going to be easier to implement with Svelte 5?

macmillen avatar Aug 15 '24 08:08 macmillen

I'm working with a lot of tree structures where svelte:self is heavily used and I regularly run into type issues because svelte:self is not typed.

macmillen avatar Aug 15 '24 08:08 macmillen

It might be possible. Since slot props are replaced by snippets and the event is replaced by callback props so there's less inferred type. I tried and the prototype seems promising.

圖片

jasonlyu123 avatar Aug 15 '24 13:08 jasonlyu123

svelte:self should probably just be removed in the future. Self-referential imports are possible and type-checked.

<!-- tree.svelte -->
<script lang="ts">
	import Tree from './tree.svelte'; // it's me, the current file!

	type TreeItem = {
		name: string;
		children?: TreeItem[];
	};

	export let node: TreeItem;
</script>

<div>{node.name}</div>
<div>
	{#each node.children ?? [] as child}
		<Tree /> <!-- Property 'node' is missing in type '{}' but required in type '{ node: TreeItem; }'. ts(2741) -->
	{/each}
</div>

brunnerh avatar Aug 15 '24 15:08 brunnerh

svelte:self should probably just be removed in the future. Self-referential imports are possible and type-checked.

Oh nice, I didn't know that. Then I would think it's best to remove svelte:self because it seems redundant.

macmillen avatar Aug 15 '24 15:08 macmillen

I'm going to close this unless anyone objects since it is now deprecated.

Zachiah avatar Jul 30 '25 04:07 Zachiah