【问题标题】:Type 'URL' is not assignable to type 'string'类型“URL”不可分配给类型“字符串”
【发布时间】:2020-07-31 12:57:34
【问题描述】:

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

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.

【问题讨论】:

  • 你能分享更多关于你的代码 sn-p 的信息吗
  • 你能分享你的 app.ts 文件吗?
  • 我在使用节点时遇到同样的错误...降级到 12.18.3 并且仍在发生
  • 你是用天龙来运行应用吗?

标签: deno


【解决方案1】:

您可能正在使用具有 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 二进制文件本身以单独的方式发布)

【讨论】:

  • 我在使用节点 12.18.3 时遇到同样的错误,有什么建议吗?
猜你喜欢
  • 2021-02-22
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
相关资源
最近更新 更多