Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser API: URL class is not recognized in Typescript

I'm using URL utility: https://developer.mozilla.org/en-US/docs/Web/API/URL and I have a problem using it in Typescript code.

Whereas I can open the console in the browser and type:

new URL("http://stackoverflow.com/question/ask")

and the code is successful, whenever I write it in Typescript file and transpile it, I get the following error:

error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

Does it mean that this class doesn't exist in Typescript? Is there any way to use it in Typescript code?

like image 640
CrazySynthax Avatar asked Jul 17 '26 16:07

CrazySynthax


1 Answers

That should be supported by TypeScript, actually that's included on lib.dom.d.ts which is included by default if your target is es5.

You can also configure it on tsconfig.json (that is the default config when using es5, but you can try to add it explicitly):

"lib": [
            "dom",
            "es5",
            "scripthost"
        ],
like image 99
José Quinto Zamora Avatar answered Jul 20 '26 07:07

José Quinto Zamora