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

type svelte:self #1229

Open
Zachiah opened this issue Nov 3, 2021 · 8 comments
Open

type svelte:self #1229

Zachiah opened this issue Nov 3, 2021 · 8 comments
Labels
feature request New feature or request

Comments

@Zachiah
Copy link

Zachiah commented Nov 3, 2021

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

@dummdidumm dummdidumm transferred this issue from sveltejs/svelte Nov 4, 2021
@dummdidumm dummdidumm added the feature request New feature or request label Nov 4, 2021
@dummdidumm
Copy link
Member

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.

@DetachHead
Copy link
Contributor

DetachHead commented Nov 24, 2022

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

@GauBen
Copy link
Contributor

GauBen commented Feb 24, 2023

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

@macmillen
Copy link

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

@macmillen
Copy link

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.

@jasonlyu123
Copy link
Member

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.

圖片

@brunnerh
Copy link
Member

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>

@macmillen
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants