Allow visibility on constructors
I think it is a pretty common pattern to have a static factory method to create a class and the constructor of this class being otherwise private so that you cannot instantiate the class unless you use the factory method.
@jbondc Is this "Architecture astronauts" any reasonable argument? You try to offend or praise the people that want this feature?
@jbondc I don't believe the term "architecture astronaut" is relevant here. Isn't that talking about people who spend more time thinking about architecture than writing code? The decision to use a private constructor can be quick and simple like using almost any feature in a language like TypeScript.
Additionally, I don't think it's "arbitrary" because it can help prevent misuse of the code. Maybe I want to remind myself or my team not to use the constructor and instead use one of the static methods or force the use of a static method to enforce a singleton. It's quick and simple and I get the proper feedback from the compiler. If that's arbitrary then you could argue that a lot of aspects of a language are arbitrary. Maybe you have more to say about it being arbitrary that you didn't express in your comment?
This is a language feature that if people don't like it's simple for them to not use.
@dsherret Not enough to document instead of enforcing yet another constraint?
It complicates multiple inheritance significantly, see #4805 (which to me seems like an afterthought right now). I've already expressed some of my thoughts in #3578 so won't bother to do it again. Came out a bit strong with astronaut, don't mean to offend anyone.
@jbondc classes with private constructors wouldn't be able to be inherited. They're essentially sealed and so inheritance, let alone multiple inheritance, shouldn't work with this.
It's much nicer to have self-documenting code than to write it externally or in a comment. That's one of the reasons I like all of TypeScript's restrictions and essentially what we're asking for in this feature—just another way to document the code by using the language itself.
Well here is another way to write your example:
module Currency {
export enum Type {
CAD = 1.3,
USD = 1,
}
class PrivateCurrency {
constructor(private value: number, private type: Type) { }
}
export function fromNumberAndType(value: number, type: Type) {
return new PrivateCurrency(value, type);
}
export function fromString(str: string) {
let value = 10;
let type = Type.CAD;
return new PrivateCurrency (value, type);
}
}
Less OO-ish but you actually have a 'genuinely' real private class.
@jbondc That's great you have found your way for private classes! Let others use another approach (exportable class with private constructor). Now you can observe that there can be features that satisfies needs of group A and do not disrupt group B work. :)
+1
+1
Is this still on the radar? The PR is hella stale!
+1
+1
+1
:+1: can be useful for factory design pattern
Apologies if this is a repeat or just late to the party, but we are using the following pattern to achieve private constructors:
interface ExampleBuilder {
Instance(): Example;
}
export interface Example {
Val(): number;
}
export let Example: ExampleBuilder = class ExampleImpl {
constructor(v: number) {
}
Val(): number {
return 42;
}
static Instance(): Example {
return new ExampleImpl(2);
}
};
let x = Example.Instance(); // OK: x has type Example
let y = new Example(5); // ERROR: Cannot use 'new' with an expression whose type lacks a call or construct signature.
Note this can be tidied up even more using the recently merged readonly modifier.
@myitcv that's pretty cool to enforce it for now... probably the best way mentioned in my opinion. It's still too verbose though and takes a few seconds to understand what's going on, which is why an access modifier would still be very nice on constructors.
Personally, I'm just marking all my future private constructors with // todo: make private once supported comments and not calling the constructors. It will be nice once this feature comes in order to get some enforcement and better documentation with an access modifier.
@dsherret
It's still too verbose though and takes a few seconds to understand what's going on
Agreed. We don't suffer too much from that cognitive load because these classes are code generated. So the interface to the programmer is actually nice and simple.
fixed by https://github.com/Microsoft/TypeScript/pull/6885
thanks @AbubakerB!
Hi, is this feature going to be released in some future version of typescript? As of now, trying to declare a private or protected constructor gives me this error in typescript 1.8.10: error TS1089: 'private' modifier cannot appear on a constructor declaration.
Ahh, nevermind. Just found out the road map that states this feature will be included in typescript 2.0.
Ahh, nevermind. Just found out the road map that states this feature will be included in typescript 2.0.
Also the milestone being set to TypeScript 2.0 and the label Fixed would indicate that it is included. Anything with a label of Fixed generally is included in master and available via npm install typescript@next.
I am using TypeScript 2.0.2 RC and still get TS1089 when I try to make a private constructor. Am I doing it wrong or is this simply not fixed?