Skip to content

Commit 359b3ec

Browse files
author
Simon Holthausen
committed
[feat] add convenience SvelteComponentConstructor type
Eases typing "this is a variable that expects a Svelte component constructor (of a certain shape)". Removes the need for SvelteComponentTyped to be an extra type so it can be deprecated in v4 and removed in v5, and SvelteComponent(Dev) can receive the same generic typings as SvelteComponetTyped in v4.
1 parent dbbac28 commit 359b3ec

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Svelte changelog
22

3+
## Unreleased
4+
5+
* Add `SvelteComponentConstructor` convenience type ([#6770](https://github.com/sveltejs/svelte/pull/6770))
6+
37
## 3.43.0
48

59
* Use export map to expose no-op versions of lifecycle functions for SSR ([#6743](https://github.com/sveltejs/svelte/pull/6743))

src/runtime/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export {
1212
tick,
1313
createEventDispatcher,
1414
SvelteComponentDev as SvelteComponent,
15-
SvelteComponentTyped
15+
SvelteComponentTyped,
16+
SvelteComponentConstructor
1617
} from 'svelte/internal';

src/runtime/internal/dev.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,22 @@ export class SvelteComponentTyped<
247247
}
248248
}
249249

250+
/**
251+
* Convenience-type to represent a Svelte component constructor.
252+
*
253+
* Example:
254+
* ```ts
255+
import ASvelteComponent from './ASvelteComponent.svelte';
256+
const ComponentClass: SvelteComponentConstructor = ASvelteComponent;
257+
new ComponentClass(..);
258+
```
259+
*/
260+
export type SvelteComponentConstructor<
261+
Props extends Record<string, any> = any,
262+
Events extends Record<string, any> = any,
263+
Slots extends Record<string, any> = any
264+
> = new (options: IComponentOptions<Props>) => SvelteComponentTyped<Props, Events, Slots>;
265+
250266
export function loop_guard(timeout) {
251267
const start = Date.now();
252268
return () => {

0 commit comments

Comments
 (0)