benjamn / ast-types

Esprima-compatible implementation of the Mozilla JS Parser API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to emit a "private" field in TypeScript?

octogonz opened this issue · comments

I can emit something like this:

class Example {
  private constructor() {
  }
  x = undefined;
}

...using code like this:

  classBody.body.push(
    classMethod.from({
      kind: 'constructor',
      key: Builders.identifier('constructor'),
      access: 'private',
      params: [],
      body: emptyBody
    })
  );
  classBody.body.push(
    Builders.classProperty.from({
      key: Builders.identifier('x'),
      value: Builders.identifier('undefined')
    })
  );

But how would I emit this (making x private)?

class Example {
  private constructor() {
  }
  private x = undefined;
}

The ClassProperty type does not seem to support access: 'private'.

Fixed by #323