#Deno seems to assume wrong return type (Puppeteer)

13 messages · Page 1 of 1 (latest)

steep vessel
#

With the following code, I get an type error which I do not expect:

const element: ElementHandle | null = await page.$('span.label-status');
const shouldBeString: string = await element?.$eval('a', (el): string => el.innerText);

On the shouldBeString I receive the following type error:

Type 'ElementHandle<any> | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

Even though $eval() returns any type: https://pptr.dev/api/puppeteer.page._eval

Anyone have an idea how to fix this?

This method runs document.querySelector within the page and passes the result as the first argument to the pageFunction.

modest forge
#
const shouldBeString = ... as string | undefined;
if (!shouldBeString) throw new Error("Eval failed");
// Should be a string now
low crescent
#

Undefined at least is very much correct: null?.eval() becomes undefined.

steep vessel
low crescent
#

To me it does look like TS should be properly determining the return value of the eval handler but I can't really say since I'm not on a computer.

steep vessel
#

Still a bit confused as to why TypeScript says $eval returns ElementHandle<any> | undefined

modest forge
steep vessel
#

No I mean specifically the ElementHandle<any> part. Undefined makes sense

modest forge
#

Ah, that should just be what puppeteer returns right?

steep vessel
#

but $eval should return : Promise<Awaited<ReturnType<Func>>>

steep vessel