Consider the following test case :
interface BaseFoo {}
interface FooAdapter {
method<F extends BaseFoo>(foo:F):string;
}
interface ConcreteFoo extends BaseFoo {
value:string;
}
class ConcreteFooAdapter implements FooAdapter {
method(foo: ConcreteFoo): string {
return foo.value;
}
}
The only error is with the method signature, where TypeScript complaints that :
Property 'value' is missing in type 'BaseFoo' but required in type 'ConcreteFoo'.
Why would value be present in BaseFoo since the generic F is supposed to extend it?
But, more importantly, how to fix this so there is no error?
Edit
Here is an alternative solution I was trying to investigate, but with similar failure:
interface BarAdapter {
method<F>(bar:F):string;
}
type Bar = {
value:string;
}
class ConcreteBarAdapter implements BarAdapter {
method(bar:Bar):string {
return bar.value;
}
}
It complaints that F is not assignable to type Bar, and I don't understand why.
FooAdapterbe a generic interface instead of havingFooAdapterhave a generic method. Like this playground link shows. With your version, callers choose the type argumentF. Does that address your question fully? If so I could write up an answer explaining; if not, what am I missing?BarAdapter'smethodmust work for anyFthe caller chooses, so a caller should be able to writebarAdapter.method(123). ButConcreteBarAdapter'smethodonly acceptsBars, so it's not a validBarAdapter. Again, I think you want to makeBarAdaptera generic interface with a specific method, instead of a specific interface with a generic method, as shown in this playground link. Does that work for you or not? If it works I will write up an answer (probably tomorrow because it's my bedtime now 😴)