Type 'URL' is not assignable to type 'string'

Viewed 4084

I'm getting this error. No idea from where.

theultimateprepper-api | Warning Implicitly using master branch https://deno.land/std/node/_fs/_fs_readlink.ts
theultimateprepper-api | Check file:///app/app.ts
theultimateprepper-api | error: TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'.
theultimateprepper-api |   Type 'URL' is not assignable to type 'string'.
theultimateprepper-api |   return new URL(url).pathname
theultimateprepper-api |                  ~~~
theultimateprepper-api |     at https://deno.land/std@v0.50.0/path/win32.ts:911:18
theultimateprepper-api | 
theultimateprepper-api | TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'.
theultimateprepper-api |   Type 'URL' is not assignable to type 'string'.
theultimateprepper-api |   return new URL(url).pathname;
theultimateprepper-api |                  ~~~
theultimateprepper-api |     at https://deno.land/std@v0.50.0/path/posix.ts:433:18
theultimateprepper-api | 
theultimateprepper-api | Found 2 errors.

1 Answers

You might be using a new version of Deno that has updated the URL constructor.

Compare implementation of fromFileUrl in std@v0.50.0 and std@master:

// v0.50.0
export function fromFileUrl(url: string | URL): string {
  return new URL(url).pathname; // <---- this line
}

// master
export function fromFileUrl(url: string | URL): string {
  url = url instanceof URL ? url : new URL(url); // <---- and this line
  if (url.protocol != "file:") {
    throw new TypeError("Must be a file URL.");
  }
  return decodeURIComponent(url.pathname);
}

Handling has changed.

Please use a more recent version of Deno standard modules that is compatible with the Deno release you are using (e.g. std@v0.62.0). You can find release versions at https://github.com/denoland/deno/tags

(This is understandably frustrating to find the right version, but since standard modules are still tagged with 0.x this means they are not yet fully stabilized, so released in a separate manner as the Deno binary itself)

Related