Skip to content

Commit fb97676

Browse files
author
ysaskia
committed
feat: improve props typing for render function
1 parent e168c19 commit fb97676

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

Diff for: types/index.d.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
// Definitions by: Rahim Alwer <https://github.com/mihar-22>
44

55
import {queries, Queries, BoundFunction, EventType} from '@testing-library/dom'
6-
import { SvelteComponent } from 'svelte/types/runtime'
6+
import { SvelteComponent, ComponentProps } from 'svelte/types/runtime'
77

88
export * from '@testing-library/dom'
99

10-
type SvelteComponentOptions = any
10+
type SvelteComponentOptions<C extends SvelteComponent> = ComponentProps<C> | {props: ComponentProps<C>}
1111

1212
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
1313

14+
type Constructor<T> = new (...args: any[]) => T;
15+
1416
/**
1517
* Render a Component into the Document.
1618
*/
17-
export type RenderResult<Q extends Queries = typeof queries> = {
19+
export type RenderResult<C extends SvelteComponent, Q extends Queries = typeof queries> = {
1820
container: HTMLElement
19-
component: SvelteComponent
21+
component: C
2022
debug: (el?: HTMLElement | DocumentFragment) => void
21-
rerender: (options: SvelteComponentOptions) => void
23+
rerender: (options: SvelteComponentOptions<C>) => void
2224
unmount: () => void
2325
} & { [P in keyof Q]: BoundFunction<Q[P]> }
2426

@@ -27,17 +29,17 @@ export interface RenderOptions<Q extends Queries = typeof queries> {
2729
queries?: Q
2830
}
2931

30-
export function render(
31-
component: typeof SvelteComponent,
32-
componentOptions?: SvelteComponentOptions,
32+
export function render<C extends SvelteComponent>(
33+
component: C,
34+
componentOptions?: SvelteComponentOptions<C>,
3335
renderOptions?: Omit<RenderOptions, 'queries'>
34-
): RenderResult
36+
): RenderResult<C>
3537

36-
export function render<Q extends Queries>(
37-
component: typeof SvelteComponent,
38-
componentOptions?: SvelteComponentOptions,
38+
export function render<C extends SvelteComponent, Q extends Queries>(
39+
component: Constructor<C>,
40+
componentOptions?: SvelteComponentOptions<C>,
3941
renderOptions?: RenderOptions<Q>,
40-
): RenderResult<Q>
42+
): RenderResult<C, Q>
4143

4244
/**
4345
* Unmounts trees that were mounted with render.

0 commit comments

Comments
 (0)