Mar*_*cky 8 casting fetch webstorm typescript
Input基于lib.dom.d.ts最新 WebStorm (181.4445.29) 获取的第一个位置参数类型是Request | string。鉴于https://github.com/github/fetch/issues/256关于将搜索查询参数传递给fetch调用(使用new URL(...))的讨论,应该如何在 TypeScript 中执行此操作?
如果没有这个断言:
const response = await fetch(<string>url, fetchOptions);
Run Code Online (Sandbox Code Playgroud)
我无法将 URL 对象传递给 fetch 方法...
或者也许是 WebStorm 的问题?
你不能传递一个URL对象。除了使编译器静默之外,这样做<string>url没有任何效果。
正如 的声明所述fetch,它接受字符串(' http://www.comtoso.com/ ')或Request对象。如果您使用该对象构建 URL URL,则可以使用以下方法获取字符串中的 URL toString:
const response = await fetch(url.toString(), fetchOptions);
Run Code Online (Sandbox Code Playgroud)