D
Deno

help

Deno seems to assume wrong return type (Puppeteer)

MVDMax van der Velde7/11/2023
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);
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'.
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?
Page.$eval() method | Puppeteer
This method runs document.querySelector within the page and passes the result as the first argument to the pageFunction.
KKyiro7/11/2023
This isn't a Deno issue but just how TypeScript works
const shouldBeString = ... as string | undefined;
if (!shouldBeString) throw new Error("Eval failed");
// Should be a string now
const shouldBeString = ... as string | undefined;
if (!shouldBeString) throw new Error("Eval failed");
// Should be a string now
AAapoAlas7/11/2023
Undefined at least is very much correct: null?.eval() becomes undefined.
MVDMax van der Velde7/11/2023
This seems to work indeed, thanks
AAapoAlas7/11/2023
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.
MVDMax van der Velde7/11/2023
Still a bit confused as to why TypeScript says $eval returns ElementHandle<any> | undefined
KKyiro7/11/2023
Beacuse you're using the ?. operator
KKyiro7/11/2023
Optional chaining (?.) - JavaScript | MDN
The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.
MVDMax van der Velde7/11/2023
No I mean specifically the ElementHandle<any> part. Undefined makes sense
KKyiro7/11/2023
Ah, that should just be what puppeteer returns right?
MVDMax van der Velde7/11/2023
but $eval should return : Promise<Awaited<ReturnType<Func>>> Yes exactly According to the docs it should be this

Looking for more? Join the community!

Recommended Posts
Creating my first Deno moduleHave never published to NPM or Denoland before, so I'm having some problems figuring shit out. I'm gSanitize multiple test stepsIs it possible to sanitize multiple steps inside of one test. I want to test my DB with somthing likFFI string corrupted?Is there anything i'm doing wrong here? I'm confused on why it's crashing on setVerisonUE5 in particTypescript Conditional Type ReturnsI don't know why but this conditional type return is just causing me grief. ```ts type InnerType<T>Parsing Hostname for TLD, domain name, and SLDdoes anyone know of any packages to parse a URL's hostname for the top level domain, domain name, anMigrating large Node project to DenoIs there any reliable way to migrate a Node project to Deno? Currently working on a large project thWhy does Deno.serve not return a promise anymore?Doesn't this cause problems for error handling?Changing GitHub username โ€“ what about deno.land/x?If I change my GitHub username and I have a couple of modules on deno.land/x, what will happen to thAbsolute Imports with OakI would like to have absolute imports in my Oak-API ... ๐Ÿ˜‰Passing initial data to WorkerDetault `Worker` doesnt seem to support `WorkerData` as params. Although using `node:worker_threads`Adding npm specifiers to deno.jsonHow do i add a specific version of an npm module to the cache?How do I enqueue_microtask from other threadso `Isolate.enqueue_microtask` is obviously not send/sync so I think the only option is to use` std:how to import svg in vite without type errors?Can't connect to http server on official alpine deno with std serve.I'm using the official alpine docker container to run deno, listening on port 8080, but a simple curcalling a javascript function and waiting for the promise with `rusty_v8````rust let result = function.call(scope, recv, &[]).expect("couldnt run"); /*i got this result whicDeno KV: subtract with `sum` operation?If I do `.sum([key], 1n)` the value is increased by 1. How can I decrease it? I tried `.sum([key], How to use declarations from `vite/client`?There are declarations: https://esm.sh/v126/vite@4.3.9/client.d.ts According to documentation, I neegetting errno with ffiI am using ffi to call `execv` and need to get the value of `errno` for failures, but canโ€™t see how Purging Kv keysI have a logger which write logs to a kv store with dual keys, something like ```ts kv.set(["logs_bWhat is the right way to bundle native modules?If I'm not able to import a native module with the built-in "import" keyword, what is the ideal way