Exploring a test case with TypeScript:
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;
}
}
An issue arises with the method
signature, as TypeScript raises an error stating :
Property 'value' is missing in type 'BaseFoo' but required in type 'ConcreteFoo'.
The query is on why value
needs to be in BaseFoo
when the generic F
should extend it?
More importantly, what would be the correct approach to resolve this without any errors?
Edit
Considering an alternative solution that faced a similar problem:
interface BarAdapter {
method<F>(bar:F):string;
}
type Bar = {
value:string;
}
class ConcreteBarAdapter implements BarAdapter {
method(bar:Bar):string {
return bar.value;
}
}
A complaint emerges indicating that F
cannot be assigned to type Bar
, which brings confusion.