3

What is the convention (standard) in TypeScript for class attributes?

In the angular 2 demo (The Heroes Tour from angular.io) all attributes are set to public :

export class Hero {
   id: number;
   name: string;
}

So they can be instanciated both ways :

var hero: Hero = new Hero();
hero.id = 0;
hero.name = "hero";

or

var hero2: Hero = {id : 0, name: "hero"};

Is there a Java style convention (like this) :

export class Hero {
   private id: number;
   private name: string;

   setId(id: number): Hero {
      this.id = id;
      return this;
   }

   setName(name: string): Hero {
      this.name = name;
      return this;
   }

   getId(): number {
      return this.id;
   }

   getName(): string {
      return this.name;
   }
}

Declaration (exemple) :

var hero: Hero = new Hero();
hero.setId(0).setName('hero');

var hero2: Hero = new Hero().setId(0).setName('hero');
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
Joshua
  • 627
  • 4
  • 9
  • 21
  • 3
    Possible duplicate of http://stackoverflow.com/questions/12827266/get-and-set-in-typescript – Peter Keuter May 30 '16 at 19:39
  • This might help maybe? http://stackoverflow.com/questions/12713659/typescript-private-members – Shane van Wyk May 30 '16 at 19:42
  • 1
    Take note that `hero instanceof Hero === true` while `hero2 instanceof Hero === false` so they are not the same. – David Sherret May 30 '16 at 20:14
  • There is no such thing as "class attributes". You probably mean "class properties". There is no such thing as "instanciating" [sic] a property. You probably mean "set". Anyway, if you wanted to know if what you wrote worked, just try it (and you will find that it does). –  May 25 '17 at 01:40

5 Answers5

1

Java is a statically compiled language. In Java, if you publish a .jar library with a class

class Foo {
  public int bar;
}

and later decide to introduce a logic around that field

class Foo {
  private int _bar;
  public int getBar() { return _bar - 1; }
  public void setBar(int v) { _bar = v + 1; }
}

any code that uses your .jar will break and will have to be updated and re-compiled. This is why it's a big no-no in Java to expose raw public fields.

TypeScript is a superset of JavaScript which is a dynamic language. All linking is dynamic. You can safely release a library with a class

class Foo {
  bar: number
}

If you later release an update

class Foo {
  private _bar: number
  get bar() { return this._bar - 1 }
  set bar(v: number) { this._bar = v + 1 }
}

your library users won't notice.

SnakE
  • 2,355
  • 23
  • 31
0

The typescript is converted into plan javascript, so... if you need something before or after set value, how to log, you to implement. If no needed, getter and setters is overkill. The instantiate and setters, for me, is the same to Java.

Example:

export class Person{
    public name: string;
    public anotherAttribute: string;
    private age: number;
    private _birth: Date;

    public set birth(birt: Date){
       this._birth = birth;
       this.age = calculateAge(birth);
    }
    .......
}


let person: Person = new Person();
person.name = "John";
......
Rafael Pizao
  • 742
  • 8
  • 21
-1

Is there a Java style convention (like this)

you can do that (and in fact I have). People use getter / setter as well:

export class Hero {
   private _id: number;

   set id(id: number) {
      this._id = id;
   }
}

However, be careful not to put too much logic in setters: https://basarat.gitbooks.io/typescript/content/docs/tips/propertySetters.html. I generally prefer explicit function calls.

var hero2: Hero = {id : 0, name: "hero"};

This is a weakness (or convenience strength) of the structural nature of TypeScript's type checking. More on this.

Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
basarat
  • 261,912
  • 58
  • 460
  • 511
  • Nobody forbids you from doing that but it's not the convention and it's overdeclarative and can get pretty ugly very quickly – Laurent Schwitter Jan 24 '18 at 19:46
  • 1
    This approach actually answers the OP's question and is accurate with a reasonable caution, and is referenced in the TypeScript docs https://www.typescriptlang.org/docs/handbook/classes.html#accessors – Marty Sep 05 '19 at 19:55
-1

is there a java style convention

Well you can do it if you want but since they're different languages you probably should use typescript conventions which usually directly access "members" via the . notation.. that is way more readable to anyone who works with typescript.. and arguably to java devs as well :) as much as I love java I do realise coding conventionally would have me typing 2 times what is useful.

In fact it's a common joke among developers that languages such as java are overdeclarative

tl;dr no.. use myObject.myMember to access members directly

-1

Is it because of the extra code around setting the individual properties that you want to use the Java style? If so you can declare the properties in the constructor of the class.

hero class

export class Hero {
 constructor(public id?: number, public name?: string) {}
}

declarations

const hero1 = new Hero(1, 'hero');
const hero2 = new Hero();
JayChase
  • 11,174
  • 2
  • 43
  • 52