25

I have interface:

export interface IFieldValue {
    name: string;
    value: string;
}

And I have a class that implements it:

class Person implements IFieldValue{
    name: string;
    value: string;
    constructor (name: string, value: string) {
        this.name = name;
        this.value = value;
    }
}

after reading this post I've thinking about refactoring:

class Person implements IFieldValue{
    constructor(public name: string, public value: string) {
    }
}

Question : In first class I have fields which by default should be as private. In second sample I can only set them as public. Is it all correct in my understanding of default Access modifiers in TypeScript?

1 Answer 1

34

Public by default. TypeScript Documentation

In following definition

class Person implements IFieldValue{
    name: string;
    value: string;
    constructor (name: string, value: string) {
        this.name = name;
        this.value = value;
    }
}

Attributes <Person>.name and <Person>.value are public by default.

as they are here

class Person implements IFieldValue{
    constructor(public name: string, public value: string) {
        this.name = name;
        this.value = value;
    }
}

Beware: Here is an incorrect way of doing it, since this.name and this.value will be regarded as not defined in the constructor.

class Person implements IFieldValue{
    constructor(name: string, value: string) {
        this.name = name;
        this.value = value;
    }
}

To make these attributes private you need to rewrite it as

class Person implements IFieldValue{
    private name: string;
    private value: string;
    constructor (name: string, value: string) {
        this.name = name;
        this.value = value;
    }
}

or equivalently

class Person implements IFieldValue{
    constructor (private name: string, private value: string) {}
}

For TypeScript 2.X since the interace has the properties as public, you need to change the private to public and also export the classes

export class Person implements IFieldValue{
    constructor (public name: string, public value: string) {}
}

which in my opinion is the most preferable way that avoids redundancy.

Sign up to request clarification or add additional context in comments.

4 Comments

If Person implements IFieldValue which has public properties 'name' and 'value' then name and value must remain public in the Person class. The two code examples you provided do not compile with TypeScript 2.x. You either change the interface and make props private or you could have private props personName and personValue like: class Person implements IFieldValue{ private personName: string; private personValue: string; constructor (public name: string, public value: string) { this.personName = name; this.personValue = value; } }
sorry disregard the part where I said: "You either change the interface and make props private" makes no sense since props are only public on an interface
fwiw, I guessed wrong when dealing with an injected parameter I wanted to declare private. Still works like above, but the @Inject comes before the private. @Inject(MyType) private _myType: MyType,
@phil_lgr I have updated the answer with important clarifications for TypeScript 2.X

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.