TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Allow visibility on constructors

Open bpasero opened this issue 11 years ago • 42 comments

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.

bpasero avatar Mar 13 '15 09:03 bpasero

@jbondc Is this "Architecture astronauts" any reasonable argument? You try to offend or praise the people that want this feature?

wgebczyk avatar Sep 20 '15 16:09 wgebczyk

@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 avatar Sep 20 '15 16:09 dsherret

@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 avatar Sep 20 '15 17:09 jbondc

@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.

dsherret avatar Sep 20 '15 17:09 dsherret

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 avatar Sep 20 '15 19:09 jbondc

@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. :)

wgebczyk avatar Sep 20 '15 19:09 wgebczyk

+1

pleerock avatar Nov 22 '15 16:11 pleerock

+1

tobiastimm avatar Nov 26 '15 11:11 tobiastimm

Is this still on the radar? The PR is hella stale!

AbubakerB avatar Nov 26 '15 13:11 AbubakerB

+1

EToreo avatar Dec 18 '15 20:12 EToreo

+1

nbransby avatar Dec 20 '15 22:12 nbransby

+1

jonaskello avatar Jan 09 '16 13:01 jonaskello

:+1: can be useful for factory design pattern

z-vr avatar Jan 24 '16 01:01 z-vr

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 avatar Feb 02 '16 10:02 myitcv

@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 avatar Feb 02 '16 14:02 dsherret

@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.

myitcv avatar Feb 02 '16 15:02 myitcv

fixed by https://github.com/Microsoft/TypeScript/pull/6885

thanks @AbubakerB!

mhegazy avatar Feb 17 '16 01:02 mhegazy

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.

agaace avatar Jun 21 '16 23:06 agaace

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.

kitsonk avatar Jun 22 '16 05:06 kitsonk

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?

stephenmartindale avatar Sep 22 '16 16:09 stephenmartindale