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

fix: support function invocation from imported *.svelte components #12061

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-days-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: support function invocation from imported `*.svelte` components
13 changes: 12 additions & 1 deletion packages/svelte/src/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
declare module '*.svelte' {
export { SvelteComponent as default } from 'svelte';
import { SvelteComponent, Component, type ComponentConstructorOptions } from 'svelte';

// Support using the component as both a class and function during the transition period
interface ComponentType {
(
...args: Parameters<Component<Record<string, any>>>
): ReturnType<Component<Record<string, any>, Record<string, any>>>;
new (o: ComponentConstructorOptions): SvelteComponent;
}
const Comp: ComponentType;
type Comp = SvelteComponent;
export default Comp;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte/tests/types/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,13 @@ render(functionComponent, {
x: ''
}
});

// --------------------------------------------------------------------------- *.svelte components

// import from a nonexistent file to trigger the declare module '*.svelte' in ambient.d.ts
// this could show an error in the future in the editor (because language tools intercepts and knows this is an error)
// but should always pass in tsc (because it will never know about this fact)
import Foo from './doesntexist.svelte';

Foo(null, { a: true });
const f: Foo = new Foo({ target: document.body, props: { a: true } });
13 changes: 12 additions & 1 deletion packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2634,7 +2634,18 @@ declare module 'svelte/types/compiler/interfaces' {
*/
type Namespace = 'html' | 'svg' | 'mathml' | 'foreign';
}declare module '*.svelte' {
export { SvelteComponent as default } from 'svelte';
import { SvelteComponent, Component, type ComponentConstructorOptions } from 'svelte';

// Support using the component as both a class and function during the transition period
interface ComponentType {
(
...args: Parameters<Component<Record<string, any>>>
): ReturnType<Component<Record<string, any>, Record<string, any>>>;
new (o: ComponentConstructorOptions): SvelteComponent;
}
const Comp: ComponentType;
type Comp = SvelteComponent;
export default Comp;
}

/**
Expand Down
Loading