I have developed a straightforward generic interface using typescript
interface DateAdapter<T> {
clone(): T;
}
and I've implemented the interface in a basic class
class StandardDateAdapter implements DateAdapter<StandardDateAdapter> {
clone: () => StandardDateAdapter;
}
Next, I have a generic Options
interface that takes a type extending DateAdapter
. I want the default type argument to be StandardDateAdapter
, so I declared it as follows:
interface Options<T extends DateAdapter<T> = StandardDateAdapter> {
until?: T;
}
However, typescript is raising an error with this setup
Type 'StandardDateAdapter' does not satisfy the constraint 'DateAdapter<T>'.
Types of property 'clone' are incompatible.
Type '() => StandardDateAdapter' is not assignable to type '() => T'.
Type 'StandardDateAdapter' is not assignable to type 'T'.
Does anyone have suggestions on how to resolve this issue? Could this be a bug? Thank you!
note: DateAdapter
is generic because I want the return type of clone()
to match the type of the class implementing DateAdapter
. Initially, I tried clone(): this;
in the DateAdapter, but typescript correctly recognized that StandardDateAdapter#clone() returning new StandardDateAdapter
is not the same as this