microsoft / microsoft/TypeScript

Generics return type of function reported not assignable.

Open
#43,176 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

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

Reproduce the diagnostic in the linked TypeScript Playground using the TypeCaseA, TypeCaseB, TypeCaseAB, and Service.func1/func2/func3 examples. Compare the generic return-type handling in func1 with the accepted func2 and func3 cases. Done means func1 produces no error while preserving the expected TypeCaseAB return type.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.