hapi-decorators icon indicating copy to clipboard operation
hapi-decorators copied to clipboard

how to use with typescript?

Open iSword007 opened this issue 6 years ago • 3 comments

how to use with typescript?can you give me a example?

@controller('/service')
class ServiceController implements Controller {
  baseUrl!: string;
  routes!: () => ServerRoute[];

  @route('get', '/')
  async re (request, h) {
    const service: ServiceModel = request.getModel('service');
    return await service.findById();
  }
}

export default ServiceController;

I have to code so that VSCode haven't error.

iSword007 avatar Jul 30 '18 04:07 iSword007

What is the error(s) you are seeing?

knownasilya avatar Jul 30 '18 04:07 knownasilya

@iSword007 - Did you manage to make it work?

@knownasilya - The error I see is the following:

Argument of type 'typeof TestController' is not assignable to parameter of type 'ControllerStatic'.
Type 'TestController' is missing the following properties from type 'Controller': baseUrl, routests(2345)

My code:

@controller('/test')
class TestController {
  constructor() {}
}

I'm using: TypeScript: Version 3.9.7 Node: v12.18.3 hapi: "^18.4.0" @types/hapi-decorators: "^0.4.11" hapi-decorators: "^2.0.0"

adancarrasco avatar Dec 23 '20 17:12 adancarrasco

For those still wandering about this issue, it can be solved by having

interface TestController extends Controller { }

in the same file as the class. The decorator will ensure that the methods are actually there.

Onimishra avatar Jun 23 '21 13:06 Onimishra