如何在 TypeScript 中将 URL 对象与 Fetch API 一起使用?
How do I use URL object with Fetch API in TypeScript?
从最新的 WebStorm (181.4445.29) 获取的第一个位置参数 Input
基于 lib.dom.d.ts
的类型是 Request | string
。
考虑到 https://github.com/github/fetch/issues/256 关于将搜索查询参数传递给 fetch
调用(使用 new URL(...)
),在 TypeScript 中应该如何做?
没有这个断言:
const response = await fetch(<string>url, fetchOptions);
我无法将 URL 对象传递给 fetch 方法...
或者是 WebStorm 的问题?
您不能传递 URL
对象。执行 <string>url
除了让编译器静音之外没有任何效果。
正如 fetch
的声明所说,它接受字符串 ('http://www.comtoso.com/') 或 Request
对象。如果您使用 URL
对象构建 URL,则可以使用 toString
:
获取字符串中的 URL
const response = await fetch(url.toString(), fetchOptions);
从最新的 WebStorm (181.4445.29) 获取的第一个位置参数 Input
基于 lib.dom.d.ts
的类型是 Request | string
。
考虑到 https://github.com/github/fetch/issues/256 关于将搜索查询参数传递给 fetch
调用(使用 new URL(...)
),在 TypeScript 中应该如何做?
没有这个断言:
const response = await fetch(<string>url, fetchOptions);
我无法将 URL 对象传递给 fetch 方法...
或者是 WebStorm 的问题?
您不能传递 URL
对象。执行 <string>url
除了让编译器静音之外没有任何效果。
正如 fetch
的声明所说,它接受字符串 ('http://www.comtoso.com/') 或 Request
对象。如果您使用 URL
对象构建 URL,则可以使用 toString
:
const response = await fetch(url.toString(), fetchOptions);