fix(types): evaluate should not unpack return unions

This commit is contained in:
Max Schmitt 2024-03-16 15:46:54 +01:00
parent 306db80d03
commit ea462618d4
2 changed files with 9 additions and 2 deletions

View file

@ -40,6 +40,6 @@ export type Unboxed<Arg> =
export type PageFunction0<R> = string | (() => R | Promise<R>); export type PageFunction0<R> = string | (() => R | Promise<R>);
export type PageFunction<Arg, R> = string | ((arg: Unboxed<Arg>) => 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 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 ElementHandleForTag<K extends keyof HTMLElementTagNameMap> = ElementHandle<HTMLElementTagNameMap[K]>;
export type BindingSource = { context: BrowserContext, page: Page, frame: Frame }; export type BindingSource = { context: BrowserContext, page: Page, frame: Frame };

View file

@ -323,6 +323,13 @@ playwright.chromium.launch().then(async browser => {
console.log(await resultHandle.jsonValue()); console.log(await resultHandle.jsonValue());
await resultHandle.dispose(); await resultHandle.dispose();
// evaluteHandle with two different return types
{
const handle = await page.evaluateHandle(() => '' as string | number);
const result = await handle.evaluate(value => value);
const assertion: AssertType<string | number, typeof result> = true;
}
await browser.close(); await browser.close();
})(); })();
@ -606,7 +613,7 @@ playwright.chromium.launch().then(async browser => {
{ {
const handle = await page.waitForSelector('*'); const handle = await page.waitForSelector('*');
const value = await handle.evaluateHandle((e: HTMLInputElement, x) => e.disabled || x, 123); 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;
} }
{ {