Open
Description
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.