deno: deno.ns URL and TypeScript dom URL incompatible constructors

The deno standard lib declaration of URL accepts string | URL as input: https://github.com/denoland/deno/blob/a08a4abac116eda498f8ad2df13b3816ec36c9ad/cli/js/lib.deno.shared_globals.d.ts#L1149

However the TypeScript dom library declaration only accepts string: https://github.com/microsoft/TypeScript/blob/master/src/lib/dom.generated.d.ts#L15920

When declaring “dom” in tsconfig.json like so:

"lib": ["dom", "dom.iterable", "esnext", "deno.ns"]

deno throws the following 2 errors:

error: TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'.
  Type 'URL' is not assignable to type 'string'.
  return new URL(url).pathname;
                 ~~~
    at https://deno.land/std@0.51.0/path/posix.ts:433:18

TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'.
  Type 'URL' is not assignable to type 'string'.
  return new URL(url).pathname
                 ~~~
    at https://deno.land/std@0.51.0/path/win32.ts:911:18

Found 2 errors.

Probably related to https://github.com/denoland/deno/issues/4234.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 32 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Also if you are using untagged imports (such as https://deno.land/std/encoding/utf8.ts), please update those to use the 0.61.0 type import i showed above

People seem to be getting slightly confused with all this, so lets try to clarify some stuff.

With the Deno 1.2.0 update, the URL constructor was changed so that you cannot pass a URL as input to the constructor, to realign this up with the web standards: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL. As with every Deno update a new std version was released, v0.61.0.

The previous versions of the std were created for older versions of deno, and thus some of them expected the old behaviour of the URL function, which is now not the case.

Solution:

There are two possible solutions here depending on where your issue lies.

If your issue lies in your dependencies, you should make an issue or pull request on the module’s github to update their code to support Deno v1.2.0 and std v0.61.0. You can work out exactly where the issue lies by running deno info --no-check deps.ts. You should also downgrade to deno v1.1.3 for now, until the dependencies are fixed.

If the issue only exists in your own code, you should just be able to update your imports to reference std v0.61.0, for example https://deno.land/std@0.61.0/encoding/utf8.ts. You won’t need to downgrade, and everything should run relatively smoothly.

It looks this is caused by the combination Deno@1.2.0 and std@0.60.0 or before.

After upgrading to 1.2.0 I am getting this error as well. Is it normal?

It is really obscure in the spec, but I found it:

while IDL requires a string as argument, a URL object stringifies to its href getter return value

So basically everyone is relying upon implicit coercion. 🤷 Seems that this has tripped up TypeScript a few times overall as well too: https://github.com/microsoft/TSJS-lib-generator/issues/670.

I say we follow TypeScript for now, and just have people complain about it and tell people to just be explicit and use String(url) or url.toString(). More explicit is better anyways.

I had the same problem and this mongodb import import { init, MongoClient } from 'https://deno.land/x/mongo@v0.6.0/mod.ts' made it, because this package uses https://deno.land/std@v0.50.0 which doesn’t exist anymore . So, if you are using mongodb, you can replace that version with https://deno.land/x/mongo@v0.9.1/mod.ts or you have an alternative here: https://deno.land/x/denodb. If you don’t use mongodb, check with deno info --no-check app.ts which file and import statement uses some packages which doesn’t exist anymore. After you remove those imports, run deno server again with flag --reload