From a9e1986cb22479f1bbb24fae2d3f981265cb5434 Mon Sep 17 00:00:00 2001 From: Dennis273 Date: Sun, 12 Dec 2021 10:46:53 +0800 Subject: [PATCH 1/3] fix(within): fix ts error when reassigning --- types/__tests__/type-tests.ts | 4 ++++ types/get-queries-for-element.d.ts | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/types/__tests__/type-tests.ts b/types/__tests__/type-tests.ts index ad47e73d..1fe04572 100644 --- a/types/__tests__/type-tests.ts +++ b/types/__tests__/type-tests.ts @@ -254,6 +254,10 @@ 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}) } /* diff --git a/types/get-queries-for-element.d.ts b/types/get-queries-for-element.d.ts index 868d6d2a..42c9be39 100644 --- a/types/get-queries-for-element.d.ts +++ b/types/get-queries-for-element.d.ts @@ -175,7 +175,7 @@ export interface Queries { [T: string]: Query } -export function getQueriesForElement( - element: HTMLElement, - queriesToBind?: T, -): BoundFunctions +export function getQueriesForElement< + T extends Queries = typeof queries, + K extends T = T, +>(element: HTMLElement, queriesToBind?: K): BoundFunctions From 9d8843f9e7b723a1a434b51f8516f021261a27da Mon Sep 17 00:00:00 2001 From: eps1lon Date: Sat, 15 Jan 2022 18:20:02 +0100 Subject: [PATCH 2/3] Test if we need one generic per re-assignment --- types/__tests__/type-tests.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/__tests__/type-tests.ts b/types/__tests__/type-tests.ts index 1fe04572..7a3212cf 100644 --- a/types/__tests__/type-tests.ts +++ b/types/__tests__/type-tests.ts @@ -258,6 +258,8 @@ export async function testWithin() { 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}) } /* From b55dde9f1c1d5d37c2525479639fcc639d81431f Mon Sep 17 00:00:00 2001 From: eps1lon Date: Sat, 15 Jan 2022 18:20:56 +0100 Subject: [PATCH 3/3] Add more comments I don't understand why this works so let's make sure this isn't easily reverted before checking git-blame --- types/get-queries-for-element.d.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/types/get-queries-for-element.d.ts b/types/get-queries-for-element.d.ts index 42c9be39..33d13de8 100644 --- a/types/get-queries-for-element.d.ts +++ b/types/get-queries-for-element.d.ts @@ -176,6 +176,7 @@ export interface Queries { } export function getQueriesForElement< - T extends Queries = typeof queries, - K extends T = T, ->(element: HTMLElement, queriesToBind?: K): BoundFunctions + QueriesToBind extends Queries = typeof queries, + // Extra type parameter required for reassignment. + T extends QueriesToBind = QueriesToBind, +>(element: HTMLElement, queriesToBind?: T): BoundFunctions