Skip to content

Recursive generic constraint #1561

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
NN--- opened this issue Dec 25, 2014 · 2 comments
Closed

Recursive generic constraint #1561

NN--- opened this issue Dec 25, 2014 · 2 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@NN---
Copy link

NN--- commented Dec 25, 2014

Didn't find this issue.
Will TypeScript allow recursive generic constraints known as CRTP:

interface B<T extends B> {}

There is a workaround but it is not nice.

interface A {
    f() : void;
}

interface B<T extends A> extends A {
}

class C implements B<C> {
    f() : void {}
}
@danquirk
Copy link
Member

Recursive types and structural types like this are not a great mix. Consider your definition of B with some actual members (or else its meaningless):

interface B<T extends B> {
    foo: string;
}

Now imagine the steps the compiler has to go through to validate whether the T in a particular B instantiation satisfies the constraint T extends B. It has to inspect the shape of B to structurally check T against, which causes another elaboration of B and asking again whether this other T extends B is satisfied, and so on. Eventually the compiler ends up having to just short circuit the comparison.

@mhegazy mhegazy added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds labels Mar 24, 2015
@mhegazy mhegazy closed this as completed Mar 24, 2015
@jbondc
Copy link
Contributor

jbondc commented Sep 21, 2015

Seems doable, I'll keep it as a note for #3496

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

4 participants