Skip to content

Make sure field initializers don't add bundle size #5316

Closed
@AbhiPrasad

Description

@AbhiPrasad

Problem Statement

When using field initializers with typescript, our bundling process does something like so:

constructor() {
  At.prototype.__init.call(this), At.prototype.__init2.call(this), At.prototype.__init3.call(this), At.prototype.__init4.call(this), At.prototype.__init5.call(this), At.prototype.__init6.call(this), At.prototype.__init7.call(this), At.prototype.__init8.call(this), At.prototype.__init9.call(this), At.prototype.__init10.call(this)
}
__init() {
  this.m = !1
}
__init2() {
  this.g = []
}
__init3() {
  this._ = []
}
__init4() {
  this.S = []
}
__init5() {
  this.k = {}
}
__init6() {
  this.O = {}
}
__init7() {
  this.j = {}
}
__init8() {
  this.T = {}
}
__init9() {
  this.D = []
}
__init10() {
  this.R = {}
}

Each __initX() call is generated from a field initializer of a class. This is a bunch of extra bytes. See an example here: https://sucrase.io/#selectedTransforms=typescript,imports&code=class%20Scope%20%7B%0A%20%20public%20a%20%3D%203%3B%0A%7D.

This is done to best match TS behaviour. See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier for more details.

Solution Brainstorm

Evidence: #5306

In the mean time, I think avoiding usage of field initializers in favour of initializing the field in a constructor is the best course of action to reduce bundle size.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions