2

I am curious if others that are using TypeScript are also implementing properties (http://www.codingdefined.com/2015/05/get-and-set-property-in-typescript.html).

I tend to like to use properties, even in my TypeScript code, but others on my team think it is overkill. For the same reasons you have properties in C# code (Why use simple properties instead of fields in C#?), I think that properties in TypeScript are good.

This isn't an "I'm curious if other people agree with me" question, but rather I am interested in knowing if others are actually doing properties in TypeScript and why or why not. Very much like the same question asked for C# properties.

I couldn't find anyone actually asking the question for TypeScript.

Community
  • 1
  • 1
meyousikmann
  • 821
  • 1
  • 9
  • 30
  • It's not a typescript feature, it's from javascript ([getters](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/get)/[setters](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/set)). I like to use them, it's pretty convenient in a lot of cases. – Nitzan Tomer Apr 18 '16 at 14:34

1 Answers1

1

I usually don't use them, unless I need them. The great thing is that we can start out using public variables and then add getters and setters as we need them.

So i usually start out like this:

class Test {
    testField: string = "";
}

And then when I realize I need a getter and setter, I change it to:

class Test {
    private _testField: string = null;
    get fieldName() : string {
        return this._testField;
        doSomething();
    }
    set fieldName(name : string) {
        doSomethingElse();
        this._testField = name;
    }
}

In Typescript we don't have to add getters and setters if all they're doing is getting and setting the value. This is in contrast to something like C#, where you would break the backwards compatibility by adding logic to the getter or setter.

Luka Jacobowitz
  • 22,795
  • 5
  • 39
  • 57
  • Well, technically in C# we don't have to add getters and setters either. We could easily make all fields public and only add properties when needed, but most seem to agree that making fields public is bad practice. It is much easier in C# due to automatic properties, but there was a time when there was no such thing as automatic properties and we wrote the getters and setters by hand. Even then, everyone seemed to agree that having public properties and private fields was good practice and I wonder why the same wouldn't apply to TypeScript. – meyousikmann Apr 18 '16 at 15:29
  • Because in C#, you're breaking backwards compatibility. :) – Luka Jacobowitz Apr 18 '16 at 15:53
  • You'll have to recompile in order for everything to work, that can be a big hassle in a lot of cases. This is because C# differentiates between a property and a public field at compile time and emits different bytecode. Typescript compiles to JS, where everything is dynamic so it's not an issue there. – Luka Jacobowitz Apr 18 '16 at 15:56