Type T does not satisfy the constraint C for type parameter P error in TypeScript
generics, typescript
Solution
Typescript interfaces are structural. That explains why when you removed the only item from `{1}` you don't get any errors anymore because any thing would be compatible with an empty interface.
That said the following should also work:
interface IScope<TViewModel> {
vm: TViewModel; // {1}
}
interface IController<TViewModel, TScope extends IScope<TViewModel>> { }
class Controller<TViewModel, TScope extends IScope<TViewModel>>
implements IController<TViewModel, TScope> { // Gets an error
}
Looks like a typescript compiler error (you might want to report it here). To be clear the following does work:
interface IScope {
vm: number; // {1}
}
interface IController<TViewModel, TScope extends IScope> { }
class Controller<TViewModel, TScope extends IScope>
implements IController<TViewModel, TScope> {
}
Problem
I'm trying to implement this in TypeScript: ``` interface IViewModel { } interface IScope<TViewModel extends IViewModel> { vm: TViewModel; // {1} } interface IController<TViewModel extends IViewModel, TScope extends IScope<TViewModel>> { } class Controller<TViewModel extends IViewModel, TScope extends IScope<TViewModel>> implements IController<TViewModel, TScope> { } // {2} ``` but I'm getting an error on line {2}: "Type 'IScope<TViewModel extends IViewModel>' does not satisfy the constraint 'IScope<TViewModel extends IViewModel>' for type parameter 'TScope extends IScope<TViewModel>'." If I go further and try to extend the Controller class: ``` interface IMainViewModel extends IViewModel { } class MainController extends Controller<IMainViewModel, IScope<IMainViewModel>> { } // {3} ``` I get a similar error on line {3}: "Type 'IScope<IMainViewModel>' does not satisfy the constraint 'IScope<TViewModel extends IViewModel>' for type parameter 'TScope extends IScope<TViewModel>'." Seems to me that this should work. What am I doing wrong? I'm pretty new to TypeScript, I mainly write code in C#, and if I write this code in C# - by just rewriting TypeScript syntax to its C# equivalent, it works just fine. The weirdest thing is that if I remove line {1}, both errors disappear.