From 1f143e5ae70e4d8a230881d75b0bd1b47eb30da8 Mon Sep 17 00:00:00 2001 From: Dennis273 Date: Sun, 16 Jan 2022 01:28:11 +0800 Subject: [PATCH] fix(within): Add extra type paramater to allow reassigning in TypeScript (#1077) Co-authored-by: eps1lon --- types/__tests__/type-tests.ts | 6 ++++++ types/get-queries-for-element.d.ts | 9 +++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/types/__tests__/type-tests.ts b/types/__tests__/type-tests.ts index ad47e73d..7a3212cf 100644 --- a/types/__tests__/type-tests.ts +++ b/types/__tests__/type-tests.ts @@ -254,6 +254,12 @@ export async function testWithin() { await container.findByRole('button', {name: /click me/i}) container.getByRole('button', {name: /click me/i}) + + let withinQueries = within(document.body) + withinQueries = within(document.body) + withinQueries.getByRole('button', {name: /click me/i}) + withinQueries = within(document.body) + withinQueries.getByRole('button', {name: /click me/i}) } /* diff --git a/types/get-queries-for-element.d.ts b/types/get-queries-for-element.d.ts index 868d6d2a..33d13de8 100644 --- a/types/get-queries-for-element.d.ts +++ b/types/get-queries-for-element.d.ts @@ -175,7 +175,8 @@ export interface Queries { [T: string]: Query } -export function getQueriesForElement( - element: HTMLElement, - queriesToBind?: T, -): BoundFunctions +export function getQueriesForElement< + QueriesToBind extends Queries = typeof queries, + // Extra type parameter required for reassignment. + T extends QueriesToBind = QueriesToBind, +>(element: HTMLElement, queriesToBind?: T): BoundFunctions