Variadic function argument not getting inferred correctly

未關閉
#45,425 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
typescript
領域
compilers

研究方向

從連結的 TypeScript Playground 範例開始,重現涉及泛型可變元組和選用 FetchQueryOptions 引數的指派錯誤。追蹤函式簽章的型別推論和可指派性行為;完成的標準是範例接受該實作,並將可變引數推論為 [string, number],同時保留選用的 overrides 參數。

由索引模型根據 Issue 內容生成。

描述

Needs Investigation

Bug Report

I am trying to create a function type where the args are some variadic number of generic args, T, and the last arg is an optional overrides variables: overrides?: FetchQueryOptions<R>, but the variadic args,T, are unable to be assigned to any type. See below for code example.

🔎 Search Terms

Generic functions variadic tuple type inference spread

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries
⏯ Playground Link

Playground link with relevant code

💻 Code
interface FetchQueryOptions<R> {
    fetchFunc?: () => R;
}

export interface IGetQueryFunc<R> {
  <T extends any[]>(
    ...args: [...rest: T, overrides?: FetchQueryOptions<R>]
  ): FetchQueryOptions<R>;
}

const getUserPostQuery: IGetQueryFunc<string> = (
  userId: string,
  postId: number,
  overrides?: FetchQueryOptions<string>
): FetchQueryOptions<string> => {
  return {
      fetchFunc: () => 'some data:' + `${userId}` + `${postId}`,
      ...overrides
  }
};
🙁 Actual behavior

I am getting the below error:

Type '(userId: string, postId: string, overrides?: FetchQueryOptions<string> | undefined) => FetchQueryOptions<string>' is not assignable to type 'IGetQueryFunc<string>'.
  Types of parameters 'userId' and 'rest' are incompatible.
    Type '[...rest: T, overrides?: FetchQueryOptions<string> | undefined]' is not assignable to type '[userId: string, postId: string, overrides?: FetchQueryOptions<string> | undefined]'.
      Variadic element at position 0 in source does not match element at position 0 in target.(2322)
🙂 Expected behavior

I expect the ...rest: T to be correctly inferred as [string, number]. and ...args to be inferred as [string, number, FetchQueryOptions<string> | undefined]

主要語言
Go
星號
111k
分支
14.4k
平均合併
1 天 19 小時
30 天內合併 PR
117

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。