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

Generics don't work properly when passing down slot props #1344

Closed
aradalvand opened this issue Jan 22, 2022 · 5 comments
Closed

Generics don't work properly when passing down slot props #1344

aradalvand opened this issue Jan 22, 2022 · 5 comments
Labels
bug Something isn't working limitation Constraints of the existing architecture make this hard to fix wontfix This will not be worked on

Comments

@aradalvand
Copy link

aradalvand commented Jan 22, 2022

Consider the following components:
Form.svelte:

<script lang="ts">
    type TValues = $$Generic;

    export let values: TValues;

    type Errors<T> = {
        [K in keyof T]: string;
    };

    let errors: Errors<TValues>;
</script>

<form>
    <slot {errors} />
</form>

SpecialForm.svelte:

<script lang="ts">
    import Form from './Form.svelte';

    type TValues = $$Generic;

    export let values: TValues;
</script>

<Form
    {values}
    let:errors
>
    <slot {errors} />
</Form>

App.svelte:

<script lang="ts">
    import SpecialForm from './SpecialForm.svelte';

    let values: {
        firstName: string;
        lastName: string;
    };
</script>

<SpecialForm
    {values}
    let:errors
>
    {errors.firstName}
</SpecialForm>

In the last component (App.svelte), the slot prop errors doesn't have the right type, it's of type Errors<unknown> even though the generic type is properly detected for values.
image

I have quite a few components that have this kind of relationship, and generics are not working as expected currently, so I'd appreciate it if you fix this. Thank you.

@aradalvand aradalvand added the bug Something isn't working label Jan 22, 2022
@aradalvand aradalvand changed the title Generics don't work properly when using slot props Generics don't work properly when passing down slot props Jan 22, 2022
@aradalvand

This comment has been minimized.

@aradalvand

This comment has been minimized.

@dummdidumm
Copy link
Member

dummdidumm commented Feb 1, 2022

Every issue you open is the most pressing you currently have. It's not like maintainers are idle and jump on every post you create. We will eventually get to answering these, and adding passive aggressive posts that are essentialy just elaborate "bump"s isn't helping. Locking and limiting the conversation on this issue, other people who have the same problem and could post actual helpful context but can't now, please thank @AradAral for that.

@sveltejs sveltejs locked and limited conversation to collaborators Feb 1, 2022
@dummdidumm
Copy link
Member

dummdidumm commented Feb 22, 2022

Problem is that our transformation of the render return is not specific enough. Component instances are instanciated with instanceOf in this case, not with the new keyword and the specific props:

// is currently this
return { ..., slots: {'default': {errors:__sveltets_1_instanceOf(Form).$$slot_def['default'].errors}}, ... }}
// would need to be something like this
return { ..., slots: {'default': {errors:new Form({ target: __sveltets_2_any(), props: {  values,}}).$$slot_def['default'].errors}}, ... }}
// or this:
declare function createComponent<Props, Events, Slots, T extends Svelte2TsxComponent<Props, Events, Slots>>(t: new (args: {target: any, props?: Props}) => T, p: Props) : T;
// ..
return { ..., slots: {'default': {errors:createComponent(Form, { values,}).$$slot_def['default'].errors}}, ... }}

Fixing this is hard because we need to apply the same logic we apply in the context of each/await (unwrapping stuff) and additionally instantiate the component.

@jasonlyu123 jasonlyu123 added the limitation Constraints of the existing architecture make this hard to fix label Feb 21, 2023
@dummdidumm
Copy link
Member

Closing since Svelte 5 replaces slots with snippets and render tags, which don't have this problem (because you're required to type it properly)

@dummdidumm dummdidumm added the wontfix This will not be worked on label Jul 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working limitation Constraints of the existing architecture make this hard to fix wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants