1

Given

function addUnderscore(...) {
  // adds `_${fieldname}` to class
}

class Foo {

  @addUnderscore
  x=1

  @addUnderscore
  y=2

}

Can I get tsc to notice that Foo._x exists because of addUnderscore and tell it what type it is?

4
  • This feels more like a job for a mixin than a class decorator... Commented Apr 10 at 15:08
  • Then I would have to wrap the whole class, rather than individual fields... Commented Apr 10 at 15:14
  • 1
    Indeed, but that's the only thing you can do in TS other than manually tweaking Foo's type independently of the decorator. ms/TS#4881 remains open, decorators don't mutate the types of a class declaration. Commented Apr 10 at 15:16
  • Yeah that's why I added that as a comment and not an answer, but it's the "best" option here for preserving type safety as jcalz mentions. Commented Apr 10 at 15:17

0