Skip to content

Incorrect compilation with static method polymorphism #18769

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

Closed
rzio opened this issue Sep 26, 2017 · 3 comments
Closed

Incorrect compilation with static method polymorphism #18769

rzio opened this issue Sep 26, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@rzio
Copy link

rzio commented Sep 26, 2017

TypeScript Version: 2.5.2

Code

export interface IFuture<TResult> {
    done(callback: (result: TResult) => void): IFuture<TResult>;
}
export interface IVoidFuture extends IFuture<void | any> {
    done(callback: (result?: void) => void): IVoidFuture;
}

export class Future<TResult> implements IFuture<TResult>{
    public static createResolved<TResult>(result?: TResult): IFuture<TResult> {
        let asyncResult: Future<TResult> = new Future<TResult>();
        return asyncResult;
    }


    public done(callback: (result: TResult) => void): Future<TResult> {
        return this;
    }
}

export class VoidFuture extends Future<void> implements IVoidFuture  {
    private static _resolvedFuture: IVoidFuture;

    public static createResolved(result?: void): IVoidFuture  {
        if (!VoidFuture._resolvedFuture) {
            VoidFuture._resolvedFuture = Future.createResolved<void>();
        }
        return VoidFuture._resolvedFuture;
    }

    public done(callback: (result?: void) => void): IVoidFuture {
        return super.done(callback);
    }
}

Expected behavior:
Should compile
Actual behavior:

file.ts(20,14): error TS2417: Class static side 'typeof VoidFuture' incorrectly extends base class static side 'typeof Future'.
  Types of property 'createResolved' are incompatible.
    Type '(result?: void) => IVoidFuture' is not assignable to type '<TResult>(result?: TResult) => IFuture<TResult>'.
      Types of parameters 'result' and 'result' are incompatible.
        Type 'TResult' is not assignable to type 'void'.
@RyanCavanaugh
Copy link
Member

This is a correct error. It's legal to write

const f: typeof Future = VoidFuture;
const r = f.createdResolved<string>(); // r: IFuture<string>

But VoidFuture is only going to return IFuture<void> in that case

@mhegazy
Copy link
Contributor

mhegazy commented Sep 26, 2017

Please see #18387 and #18465 and

@mhegazy mhegazy added the Duplicate An existing issue was already created label Sep 26, 2017
@rzio
Copy link
Author

rzio commented Sep 26, 2017

Got it

@rzio rzio closed this as completed Sep 26, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants