'Is there a way to "enchant" a class with a type?
Say I have a simple class class Foo {}. Under the hood with some Javascript magic, I know that a Foo object can do much more than it appears to be, which is complex enough that it cannot be expressed with an interface, but possibly with a type:
interface Bar { /* ... */ }
type Processor<T> = (value: T) => void;
type FooEx = { [K in keyof Bar as `${K & string}Processor`]: Processor<K> }
Is there a way I can say Foo "implements" FooEx, so I can use any Foo object as a FooEx? I know I can always cast/wrap it, but ideally I'd like to have it by using new Foo() and it should work out of the box.
Edit:
I'm sorry but the example above has oversimplified my question, which can be easily achieved by writing:
class Foo implements FooEx {}
so here is a more complex example:
type Processor<T> = (value: T) => void;
type FooEx<T> = { [K in keyof T as `${K & string}Processor`]: Processor<K> }
Now, this does not work:
class Foo<T> implements FooEx<T> {}
~~~~~~~~
// Error: A class can only implement an object type or intersection of object types with statically known members.ts(2422)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
