Can a generic class extend it's type parameter?

Topics: Language Specification
Jan 8 at 2:35 PM
It is common in C++ and C# for generic types to extend a type parameter. When I've tried to do so in VS2013 I get the error: error TS2073: Build: A class may only extend another class.

Example:

module Api {
    export class PagedCriteria<TCriteria> extends TCriteria {
        public skip: number;
        public take: number;
        public page: number;
        public pageSize: number;
    }
}
I suppose since TCriteria extends Object by default, which is an interface, the error makes sense. However, even if I make it extend a class, it still doesn't work:
module Api {
    export class CriteriaBase { }
    export class PagedCriteria<TCriteria extends CriteriaBase> extends TCriteria {
        public skip: number;
        public take: number;
        public page: number;
        public pageSize: number;
    }
}
Is there something like the c# 'where T : class' functionality for TypeScript?

I have looked through the specification and the forums, but I have not seen the issue addressed. As I said, this is a very common idiom for C++ and C# generic programming.
Coordinator
Jan 8 at 5:31 PM
Edited Jan 8 at 5:34 PM
In the first example, we don't know enough about the type variable to say that we can extend it. Just for reference, the type system assumes that a type variable without any constraints has the shape { }, so we don't have much to go on.

At first blush, the second example looks like it should work to me (assuming there isn't some subtlety involved I'm missing). It's quite possible this is a bug in the compiler.
Jan 8 at 6:53 PM
What does it matter if the initial shape is { }? The actual type parameter itself may have all kinds of content, and in any event, I'm just adding four properties regardless of the type parameter's content. The second example, that you site as a possible bug has the same shape as the default: { }, but you think that should work.

Again, this is something that is very common in C++ and C#. Why can these languages deal with it but typescript's JavaScript generator cannot?

Should I enter a bug for this issue?
Jan 8 at 9:30 PM
@kenbrubaker actually C# disallow such pattern. But TypeScript might do this. Maybe =)
Coordinator
Jan 9 at 1:49 AM
I'm actually wrong - turns out this intentionally not allowed.

We don't have a C++-style templating system, which would let you "stamp out" classes each time you have a new instance instantiated with a type variable. To TypeScript, the types Dictionary<number> and Dictionary<string> codegen to the same Dictionary constructor rather than two different constructors. In TypeScript, types are erasable - they don't have an effect on the codegen (except in a couple exceptions around modules).

Because of this, we statically have one inheritance chain.
Jan 9 at 2:37 PM
I stand corrected:

error CS0689: Cannot derive from 'T' because it is a type parameter