microsoft / microsoft/TypeScript
Generics return type of function reported not assignable.
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Bug Report
🔎 Search Terms
return value, typescript, generics
🕗 Version & Regression Information
This changed between versions 3.5.1 and 3.6.3
⏯ Playground Link
💻 Code
type TypeCaseA<A> = { data: A };
type TypeCaseB<B = string> = { data?: B; code: string; };
type TypeCaseAB<A, B = string> = TypeCaseA<A> | TypeCaseB<B>;
class Service {
getB<B = string>(code: string, data?: B): TypeCaseB<B> {
const result: TypeCaseB<B> = { data, code };
return result;
}
getA<A>(data: A): TypeCaseA<A> {
return { data };
}
func1(): TypeCaseAB<{ id: number }> {
/* The following line is blamed with:
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseAB<{ id: number; }, string>'.
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseB<string>'.
Type 'string | { id: number; }' is not assignable to type 'string'.
Type '{ id: number; }' is not assignable to type 'string'.
*/
return this.getB('1231');
}
func2(): TypeCaseAB<{ id: number }> {
const retval = this.getB('1231');
return retval;
}
func3(): TypeCaseAB<{ id: number }, string> {
return this.getB('1231', '456');
}
}
🙁 Actual behavior
func1 is tested with the following error:
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseAB<{ id: number; }, string>'.
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseB<string>'.
Type 'string | { id: number; }' is not assignable to type 'string'.
Type '{ id: number; }' is not assignable to type 'string'.
There should be no difference from func2 which is tested correct.
🙂 Expected behavior
No error reported.
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
Reproduce el diagnóstico en el TypeScript Playground enlazado utilizando los ejemplos TypeCaseA, TypeCaseB, TypeCaseAB y Service.func1/func2/func3. Compara el manejo del tipo de retorno genérico en func1 con los casos aceptados func2 y func3. Se considera terminado cuando func1 no produce ningún error y conserva el tipo de retorno TypeCaseAB esperado.
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