Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic function types cannot merge using union types #6160

Closed
falsandtru opened this issue Dec 18, 2015 · 3 comments
Closed

Generic function types cannot merge using union types #6160

falsandtru opened this issue Dec 18, 2015 · 3 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@falsandtru
Copy link
Contributor

type f = {
  (): number;
} | {
  (): string;
}

var f: f;
f(); // valid

type g = {
  <T>(): number;
} | {
  <T>(): string;
}

var g: g;
g(); // error
type f = {
  (): any;
  (v: any): any;
} | {
  (v?: any): any;
}

var f: f;
f(); // valid

type g = {
  <T>(): any;
  <T>(v: any): any;
} | {
  <T>(v?: any): any;
}

var g: g;
g(); // error
@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Dec 18, 2015
@weswigham
Copy link
Member

This is by design, apparently. We have this comment in the related code:

        // The signatures of a union type are those signatures that are present in each of the constituent types.
        // Generic signatures must match exactly, but non-generic signatures are allowed to have extra optional
        // parameters and may differ in return types. When signatures differ in return types, the resulting return
        // type is the union of the constituent return types.
        function getUnionSignatures(types: Type[], kind: SignatureKind): Signature[] {

However, I'm not sure if we have good spec/test coverage for this, and I think attempting quick info on the function call in this example seems to freeze the language service.

Though this behavior is currently by design, we could possibly? do better here.

@falsandtru
Copy link
Contributor Author

Please change this issue title if you need.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Bug A bug in TypeScript labels Feb 24, 2016
@RyanCavanaugh
Copy link
Member

This is no longer an error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants