-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcomponent-types.d.ts
61 lines (54 loc) · 1.88 KB
/
component-types.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-redundant-type-constituents */
import type {
Component as ModernComponent,
ComponentConstructorOptions as LegacyConstructorOptions,
ComponentProps,
EventDispatcher,
mount,
SvelteComponent as LegacyComponent,
SvelteComponentTyped as Svelte3LegacyComponent,
} from 'svelte'
type IS_MODERN_SVELTE = ModernComponent extends (...args: any[]) => any
? true
: false
type IS_LEGACY_SVELTE_4 =
EventDispatcher<any> extends (...args: any[]) => any ? true : false
/** A compiled, imported Svelte component. */
export type Component<
P extends Record<string, any> = any,
E extends Record<string, any> = any,
> = IS_MODERN_SVELTE extends true
? ModernComponent<P, E> | LegacyComponent<P>
: IS_LEGACY_SVELTE_4 extends true
? LegacyComponent<P>
: Svelte3LegacyComponent<P>
/**
* The type of an imported, compiled Svelte component.
*
* In Svelte 5, this distinction no longer matters.
* In Svelte 4, this is the Svelte component class constructor.
*/
export type ComponentType<C> = C extends LegacyComponent
? new (...args: any[]) => C
: C
/** The props of a component. */
export type Props<C extends Component> = ComponentProps<C>
/**
* The exported fields of a component.
*
* In Svelte 5, this is the set of variables marked as `export`'d.
* In Svelte 4, this is simply the instance of the component class.
*/
export type Exports<C> = IS_MODERN_SVELTE extends true
? C extends ModernComponent<any, infer E>
? E
: C & { $set: never; $on: never; $destroy: never }
: C
/**
* Options that may be passed to `mount` when rendering the component.
*
* In Svelte 4, these are the options passed to the component constructor.
*/
export type MountOptions<C extends Component> = IS_MODERN_SVELTE extends true
? Parameters<typeof mount<Props<C>, Exports<C>>>[1]
: LegacyConstructorOptions<Props<C>>