Skip to content

Commit 9627e35

Browse files
committed
fix(types): do not erase component type constraint
1 parent acbddfd commit 9627e35

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/__tests__/render.test-d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ describe('types', () => {
1818
await rerender({ count: 0 })
1919
})
2020

21+
test('non-components are rejected', () => {
22+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
23+
class NotComponent {}
24+
25+
// @ts-expect-error: component should be a Svelte component
26+
subject.render(NotComponent)
27+
})
28+
2129
test('invalid prop types are rejected', () => {
2230
// @ts-expect-error: name should be a string
2331
subject.render(Component, { name: 42 })

src/component-types.d.ts

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-redundant-type-constituents */
2-
import type * as Svelte from 'svelte'
2+
import type {
3+
Component as ModernComponent,
4+
ComponentConstructorOptions as LegacyConstructorOptions,
5+
ComponentProps,
6+
mount,
7+
SvelteComponent as LegacyComponent,
8+
} from 'svelte'
39

4-
type IS_MODERN_SVELTE = Svelte.Component extends (...args: any[]) => any
10+
type IS_MODERN_SVELTE = ModernComponent extends (...args: any[]) => any
511
? true
612
: false
713

814
/** A compiled, imported Svelte component. */
915
export type Component<
10-
P extends Record<string, any>,
11-
E extends Record<string, any>,
16+
P extends Record<string, any> = any,
17+
E extends Record<string, any> = any,
1218
> = IS_MODERN_SVELTE extends true
13-
? Svelte.Component<P, E> | Svelte.SvelteComponent<P>
14-
: Svelte.SvelteComponent<P>
19+
? ModernComponent<P, E> | LegacyComponent<P>
20+
: LegacyComponent<P>
1521

1622
/**
1723
* The type of an imported, compiled Svelte component.
@@ -24,17 +30,17 @@ export type ComponentType<C> = IS_MODERN_SVELTE extends true
2430
: new (...args: any[]) => C
2531

2632
/** The props of a component. */
27-
export type Props<C extends Component<any, any>> = Svelte.ComponentProps<C>
33+
export type Props<C extends Component> = ComponentProps<C>
2834

2935
/**
3036
* The exported fields of a component.
3137
*
3238
* In Svelte 5, this is the set of variables marked as `export`'d.
3339
* In Svelte 4, this is simply the instance of the component class.
3440
*/
35-
export type Exports<C> = C extends Svelte.SvelteComponent
41+
export type Exports<C> = C extends LegacyComponent
3642
? C
37-
: C extends Svelte.Component<any, infer E>
43+
: C extends ModernComponent<any, infer E>
3844
? E
3945
: never
4046

@@ -43,7 +49,6 @@ export type Exports<C> = C extends Svelte.SvelteComponent
4349
*
4450
* In Svelte 4, these are the options passed to the component constructor.
4551
*/
46-
export type MountOptions<C extends Component<any, any>> =
47-
IS_MODERN_SVELTE extends true
48-
? Parameters<typeof Svelte.mount<Props<C>, Exports<C>>>[1]
49-
: Svelte.ComponentConstructorOptions<Props<C>>
52+
export type MountOptions<C extends Component> = IS_MODERN_SVELTE extends true
53+
? Parameters<typeof mount<Props<C>, Exports<C>>>[1]
54+
: LegacyConstructorOptions<Props<C>>

0 commit comments

Comments
 (0)