Skip to content

Declaring private constructors #2883

@mhevery

Description

@mhevery

When generating .d.ts file it is sometimes nice to be able to export a class as non-instantiable. Some classes are visible to the developer, but they are never meant to be instantiated by the developer, so we would like to hide their constructor.

assume class in source which needs to be exported:

class SomeType {
  /**
   * @private()  // some way to mark a constructor as private.
   */
  constructor(somePrivateDep:SomePrivateDep) {}

  someMethod(): void {...}
}

When generating .d.ts we have two options:

  1. We can generate a class and hide a constructor:
declare class SomeType {
  someMethod(): void {...}
}
  1. or we create an interface with an injectable ref.
interface PrivateConstructor {} // declared only once in .d.ts file
declare var SomeType: PrivateConstructor; // needed to make DI work properly
interface SomeType {
  someMethod(): void {...}
}

I think we should do option no. 2, but either one is fine.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions