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

How to write a generic function whose param/return types are class constructor functions? #5163

Closed
yortus opened this issue Oct 8, 2015 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@yortus
Copy link
Contributor

yortus commented Oct 8, 2015

I have a function that takes a class constructor as a parameter, and returns another class constructor of the same type. Internally, the provided constructor is subclassed and augmented, and the subclass is returned.

I'm having trouble finding a way to express this function in a generic, strongly-typed manner. Note the //ERROR comment in the repro code below:

Repro code:

// Base type of the family of classes operated on by the augment() function
class FooClass { /*...*/ }


// ========== Works, but is not generic ==========
function augment(SuperClass: typeof FooClass): typeof FooClass {
    class SubClass extends SuperClass {
        /*...*/
    }
    return SubClass;
}
var FooClass2 = augment(FooClass);


// ========== Generic, but does not compile ==========
function genericAugment<T extends typeof FooClass>(SuperClass: T): T {
    class SubClass extends SuperClass { //ERROR: Type 'T' is not a constructor function type
        /*...*/
    }
    return SubClass;
}

I've tried a few other variations, but can't seem to find a way to express this pattern.

Is there any way to do it?

@qc00
Copy link

qc00 commented Oct 8, 2015

See also #4693

@mhegazy
Copy link
Contributor

mhegazy commented Oct 9, 2015

looks like a duplicate of #4890.

@ahejlsberg has a workaround in #4890 (comment) that should help modeling this, but ultimately you want to be able to extend a type parameter.

@mhegazy mhegazy closed this as completed Oct 9, 2015
@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 9, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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