microsoft / microsoft/TypeScript
Variadic function argument not getting inferred correctly
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
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]
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground example and reproduce the assignment error involving the generic variadic tuple and optional FetchQueryOptions argument. Trace the type inference and assignability behavior for the function signature; done means the example accepts the implementation and infers the variadic arguments as [string, number] while retaining the optional overrides parameter.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100