Skip to content

bug: must use the class type not the instance type #195

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

Closed
wants to merge 2 commits into from
Closed
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
21 changes: 11 additions & 10 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import { SvelteComponentTyped } from 'svelte/types/runtime'

export * from '@testing-library/dom'

export interface SvelteComponentOptions<P extends Record<string, any> = any> {
type SvelteComponentTypedClass<P,E,S> = typeof SvelteComponentTyped<P,E,S>;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah so for some reason, typeof doesn't really play nicely when it comes to generics. I'm not sure if TypeScript gives an escape hatch for that. But what's currently here may not work.

Copy link

@molily molily Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work for me either, TS cannot parse this. If I understand correctly, the class definition should be something like

type SvelteComponentTypedClass<P, E, S> = new (options: SvelteComponentOptions<P>) => SvelteComponentTyped<P, E, S>;

See also svelte2tsx:
https://github.com/sveltejs/language-tools/blob/1415196df7118f4361385a6450ff2756bca3ff00/packages/svelte2tsx/svelte-shims.d.ts#L56-L76

(If I use this type definition I still get clashes with the svelte2tsx's generated component type definition, since it's target: Element | ShadowRoot from svelte2tsx and target?: HTMLElement in types/index.d.ts.)


export interface SvelteComponentOptions<P extends Record<string, any> = any> {
target?: HTMLElement
anchor?: string
props?: P
context?: any
hydrate?: boolean
intro?: boolean
}
};

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

Expand All @@ -23,8 +25,7 @@ type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
*/
export type RenderResult<Q extends Queries = typeof queries> = {
container: Element
component: SvelteComponent
component: SvelteComponentTyped
component: SvelteComponentTypedClass<any,any,any>
debug: (el?: Element | DocumentFragment) => void
rerender: (options: SvelteComponentOptions) => void
unmount: () => void
Expand All @@ -36,14 +37,14 @@ export interface RenderOptions<Q extends Queries = typeof queries> {
}

export function render(
component: SvelteComponentTyped,
componentOptions?: SvelteComponentOptions,
component: SvelteComponentTypedClass<any,any,any>,
componentOptions?: SvelteComponentOptions | Record<string,unknown>,
renderOptions?: Omit<RenderOptions, 'queries'>
): RenderResult

export function render<Q extends Queries>(
component: SvelteComponentTyped,
componentOptions?: SvelteComponentOptions,
component: SvelteComponentTyped<any,any,any>,
componentOptions?: SvelteComponentOptions | Record<string,unknown>,
renderOptions?: RenderOptions<Q>,
): RenderResult<Q>

Expand All @@ -52,8 +53,8 @@ export function render<
E extends Record<string, any> = any,
S extends Record<string, any> = any
>(
component: SvelteComponentTyped<P, E, S>,
componentOptions?: SvelteComponentOptions<P>,
component: SvelteComponentTypedClass<P, E, S>,
componentOptions?: SvelteComponentOptions<P> | Record<string,unknown>,
renderOptions?: Omit<RenderOptions, "queries">
): RenderResult;

Expand Down