microsoft / microsoft/TypeScript

Variadic function argument not getting inferred correctly

Aperta
#45,425 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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]

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dall’esempio collegato di TypeScript Playground e riproduci l’errore di assegnazione che coinvolge la tupla variadica generica e l’argomento opzionale FetchQueryOptions. Traccia il comportamento dell’inferenza dei tipi e dell’assegnabilità per la firma della funzione; il lavoro è completo quando l’esempio accetta l’implementazione e inferisce gli argomenti variadici come [string, number], mantenendo il parametro overrides opzionale.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.