microsoft / microsoft/TypeScript
Variadic function argument not getting inferred correctly
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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]
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem verknüpften TypeScript Playground-Beispiel und reproduziere den Zuweisungsfehler im Zusammenhang mit dem generischen variadischen Tupel und dem optionalen FetchQueryOptions-Argument. Verfolge das Verhalten der Typinferenz und der Zuweisbarkeit für die Funktionssignatur; abgeschlossen ist die Aufgabe, wenn das Beispiel die Implementierung akzeptiert und die variadischen Argumente als [string, number] inferiert, während der optionale overrides-Parameter erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100