Skip to content

Generic function does not accept argument of type with union and intersectionΒ #44315

Open
@turtleflyer

Description

@turtleflyer

Bug Report

πŸ”Ž Search Terms

generic function union intersection type optional property

πŸ•— Version & Regression Information

  • This changed between versions 3.6.0-dev.20190723 and version 3.6.0-dev.20190724

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type A = ({ a: any } | { b: any }) & { c?: any };

declare function testA<Param extends A>(param: Param & A): Param;

declare const s1: A;

const a1 = testA(s1);
/**
 * Argument of type 'A' is not assignable to parameter of type '{ b: any; } & A'.
 *   Type '{ a: any; } & { c?: any; }' is not assignable to type '{ b: any; } & A'.
 *     Type '{ a: any; } & { c?: any; }' is not assignable to type '{ b: any; } & { a: any; } & { c?: any; }'.
 *       Property 'b' is missing in type '{ a: any; } & { c?: any; }' but required in type '{ b: any; }'.(2345)
 */

type B = ({ a: any } | { b: any }) & { c: any };

declare const s2: B;

const a2 = testA(s2); // Type of result is A but must be B

πŸ™ Actual behavior

The line const a1 = testA(s1); gives an error:

Argument of type 'A' is not assignable to parameter of type '{ b: any; } & A'.
  Type '{ a: any; } & { c?: any; }' is not assignable to type '{ b: any; } & A'.
    Type '{ a: any; } & { c?: any; }' is not assignable to type '{ b: any; } & { a: any; } & { c?: any; }'.
      Property 'b' is missing in type '{ a: any; } & { c?: any; }' but required in type '{ b: any; }'.(2345)

In the line const a2 = testA(s2); the typescript compiler infers the constanta a2 has type A

πŸ™‚ Expected behavior

The variable s1 has the type A that means function testA<Param extends A>(param: Param & A): Param accepts s1. The parameter type Param is instantiated to A.

The constant a2 has the type B since the parameter s2 has the type B.

It was a normal behavior up to version 3.6.0-dev.20190723. Since then it leads to errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions