4

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.

4
  • This is the decorator transform: babeljs.io/docs/plugins/transform-decorators . But do you really need it? Depending on how it’s supposed to work, why not just write this.service = getIService() in the constructor? Commented Oct 5, 2017 at 10:55
  • I agree. Using decorators for this seems to make it drammatically more complicated. Commented Oct 5, 2017 at 14:59
  • 3
    I posted the question to understand if it's possible, not for reviews on how it looks. Thanks. Commented Oct 5, 2017 at 20:06
  • I hope they get to make this work in ES7, it comes in really handy for injects and other utilities. Regarding the last question, I guess you can do the following jsfiddle.net/mm52n6se Commented Dec 14, 2017 at 16:31

1 Answer 1

0

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.