microsoft / microsoft/TypeScript

Variadic function argument not getting inferred correctly

Abierto
#45,425 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs Investigation
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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]

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el ejemplo vinculado de TypeScript Playground y reproduce el error de asignación relacionado con la tupla variádica genérica y el argumento opcional FetchQueryOptions. Rastrea el comportamiento de la inferencia de tipos y la asignabilidad para la firma de la función; la tarea estará terminada cuando el ejemplo acepte la implementación e infiera los argumentos variádicos como [string, number], manteniendo el parámetro opcional overrides.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.