Skip to content

feat: Add buildQueries types #301

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 1 commit into from
Jul 1, 2019
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
38 changes: 38 additions & 0 deletions typings/query-helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,41 @@ export const queryByAttribute: QueryByAttribute
export const queryAllByAttribute: AllByAttribute
export const debugDOM: (htmlElement: HTMLElement) => string
export const getElementError: (message: string, container: HTMLElement) => Error

/**
* query methods have a common call signature. Only the return type differs.
*/
type QueryMethod<Arguments extends any[], Return> = (
container: HTMLElement,
...args: Arguments
) => Return
export type QueryBy<Arguments extends any[]> = QueryMethod<
Arguments,
HTMLElement | null
>
export type GetAllBy<Arguments extends any[]> = QueryMethod<
Arguments,
HTMLElement[]
>
export type FindAllBy<Arguments extends any[]> = QueryMethod<
Arguments,
Promise<HTMLElement[]>
>
export type GetBy<Arguments extends any[]> = QueryMethod<Arguments, HTMLElement>
export type FindBy<Arguments extends any[]> = QueryMethod<
Arguments,
Promise<HTMLElement>
>

export type BuiltQueryMethods<Arguments extends any[]> = [
QueryBy<Arguments>,
GetAllBy<Arguments>,
GetBy<Arguments>,
FindAllBy<Arguments>,
FindBy<Arguments>
]
export const buildQueries: <Arguments extends any[]>(
queryByAll: GetAllBy<Arguments>,
getMultipleError: (container: HTMLElement, ...args: Arguments) => string,
getMissingError: (container: HTMLElement, ...args: Arguments) => string,
) => BuiltQueryMethods<Arguments>