Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(types): evaluate should not unpack return unions (#29971)

Max Schmitt0db1d40abccb
diff --git a/packages/playwright-core/types/structs.d.ts b/packages/playwright-core/types/structs.d.ts
index eadbb2de01..9c621cff35 100644
--- a/packages/playwright-core/types/structs.d.ts
+++ b/packages/playwright-core/types/structs.d.ts
@@ -40,6 +40,6 @@ export type Unboxed<Arg> =
export type PageFunction0<R> = string | (() => R | Promise<R>);
export type PageFunction<Arg, R> = string | ((arg: Unboxed<Arg>) => R | Promise<R>);
export type PageFunctionOn<On, Arg2, R> = string | ((on: On, arg2: Unboxed<Arg2>) => R | Promise<R>);
-export type SmartHandle<T> = T extends Node ? ElementHandle<T> : JSHandle<T>;
+export type SmartHandle<T> = [T] extends [Node] ? ElementHandle<T> : JSHandle<T>;
export type ElementHandleForTag<K extends keyof HTMLElementTagNameMap> = ElementHandle<HTMLElementTagNameMap[K]>;
export type BindingSource = { context: BrowserContext, page: Page, frame: Frame };
diff --git a/utils/generate_types/test/test.ts b/utils/generate_types/test/test.ts
index 05ede91adf..e13c2ac43c 100644
--- a/utils/generate_types/test/test.ts
+++ b/utils/generate_types/test/test.ts
@@ -323,6 +323,20 @@ playwright.chromium.launch().then(async browser => {
console.log(await resultHandle.jsonValue());
await resultHandle.dispose();
+ // evaluteHandle with two different return types (JSHandle)
+ {
+ const handle = await page.evaluateHandle(() => '' as string | number);
+ const result = await handle.evaluate(value => value);
+ const assertion: AssertType<string | number, typeof result> = true;
+ }
+ // evaluteHandle with two different return types (ElementHandle)
+ {
+ const handle = await page.evaluateHandle(() => '' as any as HTMLInputElement | HTMLTextAreaElement);
+ await handle.evaluate(element => element.value);
+ const assertion: AssertType<playwright.ElementHandle<HTMLInputElement | HTMLTextAreaElement>, typeof handle> = true;
+ }
+
+
await browser.close();
})();
@@ -606,7 +620,7 @@ playwright.chromium.launch().then(async browser => {
{
const handle = await page.waitForSelector('*');
const value = await handle.evaluateHandle((e: HTMLInputElement, x) => e.disabled || x, 123);
- const assertion: AssertType<playwright.JSHandle<boolean> | playwright.JSHandle<number>, typeof value> = true;
+ const assertion: AssertType<playwright.JSHandle<boolean | number>, typeof value> = true;
}
{