microsoft / microsoft/TypeScript

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

Open
#44,315 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and reproduce the two generic calls using versions 3.6.0-dev.20190723 and 3.6.0-dev.20190724. Trace the compiler behavior for union and intersection constraints. Done means testA accepts s1 and infers a2 as B without introducing new errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.