gpt4 book ai didi

javascript - typescript 对象在参数中休息打字

转载 作者:搜寻专家 更新时间:2023-10-30 21:28:39 25 4
gpt4 key购买 nike

我对 Typescript 有点陌生,我可以对 90% 的代码库进行打字。但是当涉及到 rest/spread 运算符时,我完全不知所措。我今天在我们的代码中偶然发现了这个(我没有写这个)但我确实发现它不起作用:

interface searchClient {
searchForValues({
name,
query,
...qp,
}: {
name: string;
query: string;
qp: QpInterface; //???this part doesn't work
}): Promise<any>;
}

interface QpInterface {
page?: number;
hits?: number;
attributes?: string;
}

它不起作用,因为 qp 在键入时指定键的名称,而我想键入的是 ...qp 部分。 ...qp 中的每个键/值对都由 QpInterface 输入。

这是一个示例函数调用:

this.searchClient.searchForValues({
name: 'title',
query: 'little sheep',
page: 5,
hits: 2
});

我在文档中找不到太多相关信息。我试过放置 ...qp: QpInterface;...QpInterface; 但没有用。在参数中键入 ...qp 的最佳方式是什么?

最佳答案

Until spread types are implemented in TypeScript , 你可以使用 intersection type为此:

interface searchClient {
searchForValues({
name,
query,
...qp,
}: {
name: string;
query: string;
}
& QpInterface): Promise<any>;
}

关于javascript - typescript 对象在参数中休息打字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47319550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com