microsoft / microsoft/TypeScript

Generics return type of function reported not assignable.

Abierto
#43,176 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: check: Type Inference
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

https://www.typescriptlang.org/play?ts=3.6.3#code/C4TwDgpgBAKuEGECGBnCBBAPOgfFAvFAN5QAmSwSAXFOlAL4DcAUKJLPMmgEKbcFQUwAE4BLAHYBzPIRLlKAfhrdGUAMYB7UhBpCxU1U1bwOkLhl7oANFH6E9E6QLhnUGbHgA+pxG97ccFmY1ABtUFCgAZQhhADdRNWgiZihUqEkIYH8BBykcAApNbV0RRxt5JCVbAEoaF18ePjxktNb1DXEhKGEIFABXEOA6zj8mgTkKJBsi6CM2tJ7gPuFxbt6B4BZW+mYUtIzgLFx8ipp0Wp9zI+a9+cXl1YnKBi20nduoADM+8TUARnyF3qV14JFEpBo4j6AFsAEYxBg3eape4rKDAAAWohQADoDtx8gByP4AJgAzH9CdVXql3q1vr8SYDhq40OhQVBwZCYfDhIjiB9WppOsA1sBYkgQgJMdi8ZkCcTyZTqYKFpkHmKJSEaQxdvSfmoyczLm52ZgwRCoFC4Qj6DZck4WsjUasZbj8UTSRTCTZCQAWACsADYqTqdvQgA

💻 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

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

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.