-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte5: unexpected Component
type errors
#12627
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
Comments
Component
type to pass component as function argument is a type errorComponent
type errors
Component
type errorsComponent
type errors
I'm creating a small repo that contains what works and should continue to work and what I expected to work (imo), but doesn't. |
Title.svelte<script lang="ts">
import { onMount } from 'svelte';
const { title }: { title: string } = $props();
let _isMounted = false;
export function isMounted() {
return _isMounted;
}
onMount(() => {
_isMounted = true;
});
</script>
<h1>{title}</h1> doesn't work (all of these are type errors)<script lang="ts">
import type TitleType from '$lib/components/Title.svelte';
import Title from '$lib/components/Title.svelte';
import type { Component, ComponentProps } from 'svelte';
type ComponentMap = {
title: TitleType;
};
const inferredMap = {
title: Title
};
const renamedFromTypeMap: ComponentMap['title'] = Title;
const fromFunction = getFromFunction(Title);
const fromFunctionWithAny = getFromFunctionWithAny(Title);
const fromFunctionInferred = getFromFunction(inferredMap.title);
const props: ComponentProps<typeof Title> = {
title: 'test'
};
doSomething(Title, { title: 'something' });
doSomething<TitleType>(Title, { title: 'something' });
doSomething<typeof Title>(Title, { title: 'something' });
doSomething<ComponentMap['title']>(Title, { title: 'something' });
function getFromFunction<T extends Component>(component: T) {
return component;
}
// in the docs for ComponentProps we see:
// * function withProps<TComponent extends Component<any>>(
// * component: TComponent,
// * props: ComponentProps<TComponent>
// * ) {};
// but it still doesnt work, <any, any, any> works tho
function getFromFunctionWithAny<T extends Component<any>>(component: T) {
return component;
}
function doSomething<TComponent extends Component>(
component: TComponent,
props: ComponentProps<TComponent>
) {
return component;
}
</script> works and should continue to work (imo)<script lang="ts">
import Title from '$lib/components/Title.svelte';
import type TitleType from '$lib/components/Title.svelte';
import type { Component, ComponentProps } from 'svelte';
const renamedTitle = Title;
const inferredMap = {
title: Title
};
type ComponentMap = {
title: typeof Title;
};
const renamedTitleFromTypeMap: ComponentMap['title'] = Title;
const renamedTitleFromVar: ComponentMap['title'] = renamedTitle;
const fromFunction = getFromFunction(Title);
const fromFunctionWithMap = getFromFunction(inferredMap.title);
const props: ComponentProps<Title> = {
title: 'test',
// @ts-expect-error - its not a prop
isMounted() {
return true;
}
};
const props2: ComponentProps<TitleType> = {
title: 'test'
};
function getFromFunction<TComponent extends Component<any, any, any>>(component: TComponent) {
return component;
}
</script> |
This mostly works as expected, there are a few mistakes in your code:
Here's the corrected code: <script lang="ts">
import Title from './Title.svelte';
import type { Component, ComponentProps} from 'svelte';
type ComponentMap = {
title: typeof Title;
};
const inferredMap = {
title: Title
};
const renamedFromTypeMap: ComponentMap['title'] = Title;
const fromFunction = getFromFunction(Title);
const fromFunctionWithAny = getFromFunctionWithAny(Title);
const fromFunctionInferred = getFromFunction(inferredMap.title);
const props: ComponentProps<typeof Title> = {
title: 'test'
};
doSomething(Title, { title: 'something' });
doSomething<typeof Title>(Title, { title: 'something' });
doSomething<ComponentMap['title']>(Title, { title: 'something' });
function getFromFunction<T extends Component<any, any>>(component: T) {
return component;
}
// in the docs for ComponentProps we see:
// * function withProps<TComponent extends Component<any>>(
// * component: TComponent,
// * props: ComponentProps<TComponent>
// * ) {};
// but it still doesnt work, <any, any, any> works tho
function getFromFunctionWithAny<T extends Component<any, any>>(component: T) {
return component;
}
function doSomething<TComponent extends Component<any, any>>(
component: TComponent,
props: ComponentProps<TComponent>
) {
return component;
}
</script> That said, there's one bug in the type of |
language tools has to type its own shape for backwards compatibility, and it currently doesn't include the `$on` and `$set` methods, which means without widening the type as done here you would get a "this shape is not accepted" type error when passing it to `ComponentProps` closes #12627
language tools has to type its own shape for backwards compatibility, and it currently doesn't include the `$on` and `$set` methods, which means without widening the type as done here you would get a "this shape is not accepted" type error when passing it to `ComponentProps` closes #12627
@dummdidumm I'm concerned about two things here:
Edit: Also to get the types of exported functions on component we have to do |
Describe the bug
If you have a component (lets call it
First
), the followings will result in a type error:for the last one, if you do something like this instead, it will work though:
Another usage example:
I might be using it wrong, but as a replacement for
SvelteComponent
its giving me issues.Reproduction
read description
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: