I have this interface:
interface DataAdapterInterface {
init(url: string): void
execute(command: string, values?: string[]): Promise<KeyValue<any>>
insert(items: KeyValue<any>[]): Promise<KeyValue<any>>
find(items: KeyValue<any>[]): Promise<KeyValue<any>>
update(items: KeyValue<any>[]): Promise<boolean>
remove(items: KeyValue<any>[]): Promise<boolean>
}
and also a simple function for decoration:
export function protocol(protocol: string) {
return function (constructor: Function) {
constructor['__protocol'] = protocol;
};
}
then I made a class implementing this interface:
@protocol("mysql")
export class MySQLAdapter implements DataAdapterInterface {
private url: DatabaseUrl;
init(url: string): void {}
execute(command: string): Promise<any> {}
insert(item: KeyValue<any[]>): Promise<KeyValue<any>> {}
find(item: KeyValue<any[]>): Promise<KeyValue<any>> {}
update(item: KeyValue<any[]>): Promise<boolean> {}
remove(item: KeyValue<any[]>): Promise<boolean> {}
}
and I have this function, that receive the said class.
export function register(adapter: DataAdapterInterface) {
ConnectionFactory.register(adapter['__protocol'], adapter)
}
Now, I got an error noting that MySqlAdapter
is not DataAdapterInterface
when I do this:
register(MySQLAdapter);
The JavaScript runs fine without problem, and if I put any
as parameter, the TypeScript compiler would be completed without error. But I want to limit the parameter to only class that implements DataAdapterInterface
.
Is there a way, to pass MySQLAdapter
class as a parameter, without making an instance to it other than any
type?
2