microsoft / microsoft/TypeScript

ThisType would disturb Generics' inferring type when assign an expression to a type variable

Abierto
#39,848 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs Investigation
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

TypeScript Version: 3.9.2, and it does exist in 'Nightly' version, too.

Search Terms: ThisType, Generics, Inferring problem

Code

type customFunc = (...args: any) => any

declare function SimpleVue<
  D,
  C extends { [K in string]: customFunc },
  M = { [K in string]: customFunc },
  CC = { [K in keyof C]: ReturnType<C[K]> },
  ReturnedD = D extends customFunc ? ReturnType<D> : never,
>(option: {
  data: D,
  methods: M,
  computed: C
} & ThisType<M & ReturnedD & CC>): ReturnedD

const instance = SimpleVue({
  data() {
    return {
      firstname: 'Type',
      lastname: 'Challenges',
      amount: 10,
    }
  },
  computed: {
    fullname() {
      return this.firstname + ' ' + this.lastname
    }
  },
  methods: {
    hi() {
      alert(this.fullname.toLowerCase())
    }
  }
});

Expected behavior:

We expect ReturnedD = D extends customFunc ? ReturnType<D> : never infers ReturnedD to ReturnType<D> at runtime since D fits the shape of customFunc:

() => {
    firstname: string;
    lastname: string;
    amount: number;
}

And the code should show no error in TypeScript environment.

Actual behavior:

The actual behavior is that ReturnedD is inferred to never type since D extends customFunc here is false at runtime.

You can also check it with the returned type of instance as well.

Playground Link:

Check out the link here.

Personal speculation:

If you look at the demo above, you can do following steps to remove the error hint and confirm ReturnedD's correction:

  1. Remove ReturnedD from ThisType<M & ReturnedD & CC>;
  2. Save the file;
  3. Error disappeared on this.firstname, etc;
  4. The returned type of instance inferred from TypeScript is as expected and would not be never;

I also reproduced it with TypeScript playground.

We can confirm that:

  1. ReturnedD runs well in inferring type at runtime;
  2. The wrong type may be caused by some mechanism inside ThisType;

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 la reproducción en el playground de TypeScript 3.9.2 y compara la inferencia con y sin ReturnedD en ThisType<M & ReturnedD & CC>. Rastrea cómo ThisType afecta al tipo condicional D extends customFunc ? ReturnType : never. Se considera completado cuando el ejemplo no muestra errores y la instancia deja de inferir ReturnedD como never.

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.