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

Method overloading fails to resolve to correct method #6265

Open
AlexMSmithCA opened this issue May 8, 2018 · 1 comment
Open

Method overloading fails to resolve to correct method #6265

AlexMSmithCA opened this issue May 8, 2018 · 1 comment

Comments

@AlexMSmithCA
Copy link

AlexMSmithCA commented May 8, 2018

I have an overloaded method that has two distinct signatures. One signature is a generic that returns the invoked argument (e.g. returns some element in an encapsulation container). If I construct a type that is a union of this generic type (e.g. Encapsulator<number> | Encapsulator<string>), I see some odd behavior. If I removed the un-used overloaded method signature, there are no errors.


Simplified example on flow.org/try. Remove line 12 and type checking succeeds.

type Encapsulator<T> = {
  (): T
};
function encapsulate<TToEncapsulate>(obj: TToEncapsulate): Encapsulator<TToEncapsulate> {
  return () => obj;
}

type ExtractReturnType = <V>(() => V) => V;

declare class Deencapsulator {
  provide<T: Encapsulator<*>>(encapsulated: T): $Call<ExtractReturnType, T>,
  provide(num: number): number
}

const instance = new Deencapsulator();

type EncapsulatedOrNumber = Encapsulator<string> | Encapsulator<number>;
const whoKnows: EncapsulatedOrNumber = ({}: any);
const stringOrNumber = instance.provide(whoKnows);

Yields the following error:

19: const stringOrNumber = instance.provide(whoKnows);
                                            ^ Cannot call `instance.provide` because number [1] is incompatible with string [2] in type argument `T` [3] of type argument `T`.
References:
17: type EncapsulatedOrNumber = Encapsulator<string> | Encapsulator<number>;
                                                                    ^ [1]
17: type EncapsulatedOrNumber = Encapsulator<string> | Encapsulator<number>;
                                             ^ [2]
1: type Encapsulator<T> = {
                     ^ [3]

Commenting out provide(num: number): number yields no errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants