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

This commit is contained in:
Max Schmitt 2024-03-18 16:42:33 +01:00 committed by GitHub
parent 306db80d03
commit 0db1d40abc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View file

@ -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 };

View file

@ -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;
}
{