Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning Typescript constructor parameters

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?

like image 902
demo Avatar asked Jan 29 '17 16:01

demo


People also ask

How do you define a constructor in TypeScript?

The TypeScript docs have a great example of constructor usage: class Greeter { greeting: string; constructor(message: string) { this. greeting = message; } greet() { return "Hello, " + this. greeting; } } let greeter = new Greeter("world");

Which feature is automatic assignment of constructor parameter in TypeScript?

The TypeScript has an automatic assignment of constructor parameters that is called “Parameter Property”.

Are constructors required in TypeScript?

Classes in TypeScript do not require you to explicitly write a constructor. However if you are extending a base class you will need to create a constructor to call super() at a minimum.


1 Answers

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.

like image 149
Maciej Caputa Avatar answered Oct 07 '22 16:10

Maciej Caputa