Skip to content

private constructor(){}, It is necessary to also explicitly limit instantiation in the compiled js #61736

Closed as not planned
@sunmoon-a11y

Description

@sunmoon-a11y

🔍 Search Terms

class A{
    // Disallow external instantiation
    private constructor() {
    }
}

After compilation

var A = /** @class */ (function () {
  function A() {
  }
  return A;
}());
var A = /** @class */ (function () {
  function A() {
    // To prohibit external instantiation, the following method is more friendly
    // This is added manually, can it be automatically generated?😊😊😊
    // This is more in line with the characteristics of `private constructor`
    if (new.target === A) { // es6
      throw Error('Prohibit instantiation')
    }
    
    if (this.constructor === A) { // old version
      throw new Error("Prohibit instantiation");
    }
  }
  return A;
}());

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions