簡體   English   中英

Typescript對象在參數中進行其余輸入

[英]Typescript object rest typings in argument

我對Typescript有點陌生,我可以為90%的代碼庫進行打字。 但是當涉及到休息/分散操作員時,我絕對感到茫然。 我今天在我們的代碼中遇到了這個(我沒有寫這個),但是我確實發現它不起作用:

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的最佳方法是什么?

在TypeScript中實現擴展類型之前 ,您可以為此使用交集類型

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM