microsoft / microsoft/TypeScript
ThisType would disturb Generics' inferring type when assign an expression to a type variable
Nadie ha tomado este issue todavía.
- 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:
- Remove
ReturnedDfromThisType<M & ReturnedD & CC>; - Save the file;
- Error disappeared on
this.firstname, etc; - The returned type of
instanceinferred from TypeScript is as expected and would not benever;
I also reproduced it with TypeScript playground.
We can confirm that:
ReturnedDruns well in inferring type at runtime;- The wrong type may be caused by some mechanism inside
ThisType;
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- 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