Yann Olaf
Yann Olaf

Reputation: 597

TypeScript curious error with generics and inheritance. What's wrong?

I am trying to build some base classes for my current project. Currenty I am getting this compiler error:

Type 'IConcreteScope' does not satisfy the constraint 'IEntityScope' for type parameter 'TScope extends IEntityScope'.

When I try to run this code:

// scope 
export interface IScope {
    context: any;
}
export class ContextBase<TScope extends IScope> {
    scope: TScope;
} 
// entity
export interface IEntity {
    id: string;
}
export interface IEntityScope<TEntity extends IEntity> extends IScope {
    entity: TEntity;    
}
export class EntityContextBase<TEntity extends IEntity, TScope extends IEntityScope<TEntity>> {
    operation(entity: TEntity) {...}    
}
// concrete 
export interface IConcreteEntity extends IEntity {
    name: string;
}
export interface IConcreteScope extends IEntityScope<IConcreteEntity> {
    someprop: boolean;
}
// this is the problem: EntityContextBase<IConcreteEntity,IConcreteScope>
export class ConcretContext extends EntityContextBase<IConcreteEntity,IConcreteScope> {

} 

Here the link to the TypeScript playground code

What am I doing wrong?

Upvotes: 1

Views: 663

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220954

The code is fine; it's a bug in the compiler. This compiles without error in the latest sources from the develop branch.

Upvotes: 1

Related Questions