首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >URLSearchParams不接受字符串

URLSearchParams不接受字符串
EN

Stack Overflow用户
提问于 2022-06-06 19:16:32
回答 1查看 761关注 0票数 0
代码语言:javascript
复制
const sortString = req.query.sort as string
const params = Object.fromEntries(new URLSearchParams(sortString))

现在,当我开始执行时,我得到:

代码语言:javascript
复制
declare var URLSearchParams: {
    prototype: URLSearchParams;
    new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
    toString(): string;
};

现在,我很困惑为什么它不能处理类型记录,因为它应该接受一个字符串。

How to convert URL parameters to a JavaScript object?

代码语言:javascript
复制
No overload matches this call.
  Overload 1 of 2, '(entries: Iterable<readonly [PropertyKey, any]>): { [k: string]: any; }', gave the following error.
    Argument of type 'URLSearchParams' is not assignable to parameter of type 'Iterable<readonly [PropertyKey, any]>'.
      Property '[Symbol.iterator]' is missing in type 'URLSearchParams' but required in type 'Iterable<readonly [PropertyKey, any]>'.
  Overload 2 of 2, '(entries: Iterable<readonly any[]>): any', gave the following error.
    Argument of type 'URLSearchParams' is not assignable to parameter of type 'Iterable<readonly any[]>'.
      Property '[Symbol.iterator]' is missing in type 'URLSearchParams' but required in type 'Iterable<readonly any[]>'.ts(2769)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-06 19:53:28

错误声明传递给fromEntries的参数必须是可迭代的,而不是。从TS定义来看,URLSearchParams在https://github.com/microsoft/TypeScript/blob/2f13eba42c76d02f2e5c79060d6018ee8c7fecc9/lib/lib.dom.iterable.d.ts#L285中被定义为可迭代的,所以这个定义很可能没有加载到项目的任何TS配置中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72522489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档