Parameter decorators with Babel (like in Typescript)

Viewed 1157

is there way to use parameter decorators with Babel as it is possible in Typescript.

sample of a typescript code:

class A {
   constructor(@Inject('IService') private service: IService) {

   }
}

I want to write this code with Babel:

class A {
   constructor(@Inject('IService') service) {
      this.service = service;
   }
}

I haven't found any info on this question yet. Is there like a plugin in a certain preset that does exactly this?

If there's not way to do that without crutches, is there way to use decorators on constructors ? Example

class A {
   @Inject('IService')
   constructor(service) {
      this.service = service;
   }
}

Thanks.

1 Answers

Babel does not currently support this feature

You now have two options:

  • use babel-plugin-parameter-decorator instead
  • use tsc

Unfortunately, the first option is not perfect in detail, as the following example illustrates:

@Get('/:id')
getUser(@Param("id") id: number) {
  cosnole.log(typeof id); // string
}
Related