不能将“ URL”类型分配给“字符串”类型

时间:2020-07-24 00:23:59

标签: deno

我收到此错误。不知道从哪里来。

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 个答案:

答案 0 :(得分:0)

您可能正在使用具有updated the URL constructor的Deno新版本。

比较std@v0.50.0std@masterfromFileUrl的实现:

// 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);
}

处理方式已更改。

请使用与您正在使用的Deno版本兼容的Deno标准模块的最新版本(例如std@v0.62.0)。您可以在https://github.com/denoland/deno/tags

中找到发行版本。

(很难找到合适的版本,这是可以理解的,但是由于标准模块仍然带有0.x标记,这意味着它们尚未完全稳定,因此以Deno二进制本身的形式单独发布)