I’m trying to make readable code for my API calling module and I want to have this convention:
@API
export class Users {
async getUser(id: number) {
// some stuff...
}
}
import { Users } from 'users.ts';
const data = await Users.getUser(1);
I tried to write an API decorator to return and export instances of the API call group class. The code works when I use type any for the class but I don’t want to use type any. there is a problem with types.
The API decorator:
function API<T>(constructor: new (...args: any[]) => T) {
return new constructor();
}
@API // Error: TS1270: Decorator function return type Namespace is not assignable to type void | typeof Namespac
export class Namespace {
getNamespaces() {
console.log('hello namespace');
}
}
Namespace.getNamespaces(); // Error: TS2339: Property getNamespaces does not exist on type typeof Namespace
What should I do to fix this?