Skip to content

fix(TS): add queries typings #259

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

Merged
Merged
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
25 changes: 19 additions & 6 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
import {getQueriesForElement} from 'dom-testing-library'
import {queries, BoundFunction} from 'dom-testing-library'

export * from 'dom-testing-library'

type GetsAndQueries = ReturnType<typeof getQueriesForElement>
interface Query extends Function {
(container: HTMLElement, ...args): HTMLElement[] | HTMLElement | null
Copy link
Contributor

Choose a reason for hiding this comment

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

@chochihim @kentcdodds heads up, the ...args expression here causes TS builds to fail when --noImplicitAny is set.

Type error: Rest parameter 'args' implicitly has an 'any[]' type.  TS7019

    4 |
    5 | interface Query extends Function {
  > 6 |   (container: HTMLElement, ...args): HTMLElement[] | HTMLElement | null
      |                            ^
    7 | }
    8 |
    9 | interface Queries {

Copy link
Member

Choose a reason for hiding this comment

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

😬

Could you please open a PR to fix this @joeporpeglia.

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha! #260

}

interface Queries {
[T: string]: Query
}

export interface RenderResult extends GetsAndQueries {
export type RenderResult<Q extends Queries = typeof queries> = {
container: HTMLElement
baseElement: HTMLElement
debug: (baseElement?: HTMLElement | DocumentFragment) => void
rerender: (ui: React.ReactElement<any>) => void
unmount: () => boolean
asFragment: () => DocumentFragment
}
} & {[P in keyof Q]: BoundFunction<Q[P]>}

export interface RenderOptions {
export interface RenderOptions<Q extends Queries = typeof queries> {
container?: HTMLElement
baseElement?: HTMLElement
hydrate?: boolean
queries?: Q
}

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

/**
* Render into a container which is appended to document.body. It should be used with cleanup.
*/
export function render(
ui: React.ReactElement<any>,
options?: RenderOptions,
options?: Omit<RenderOptions, 'queries'>,
): RenderResult
export function render<Q extends Queries>(
ui: React.ReactElement<any>,
options: RenderOptions<Q>,
): RenderResult<Q>

/**
* Unmounts React trees that were mounted with render.
Expand Down