Typescript: Auto-Property support

Created on 27 May 2016  路  9Comments  路  Source: microsoft/TypeScript

Hi,
Please make typescript auto-property support like C# to easier use.

Thanks.

Duplicate Suggestion

All 9 comments

Can you give more detail? What would you like to write in TypeScript, and how would you have to write it today?

Today, we must use this syntax:

class foo {
    private _bar:Boolean = false;
    get bar():Boolean {
        return this._bar;
    }
    set bar(value:Boolean) {
        this._bar = value;
    }
}

it's better use this like syntax:

class foo {
    get bar():Boolean;
    set bar(value:Boolean);
}

or

class foo {
    set get bar():Boolean;
}

or

class foo {
    property bar():Boolean {get;set;};
}

C# implements this option. Take a look at:
https://msdn.microsoft.com/en-us/library/bb384054.aspx

Thanks

Or, as an alternative :

class foo {
  constructor(private bar: boolean) {}
}

should both initialize the bar field _and_ generate the accessors for it. (but what if I only want the getter ...?)

Essentially a duplicate of #7628 (which was out of scope)

I don't understand what the use case is. The motivations for using properties instead of fields in C# generally aren't applicable to JavaScript/TypeScript.

I don't understand what the use case is. The motivations for using properties instead of fields in C# generally aren't applicable to JavaScript/TypeScript.

Pedantically, this is called the 'uniform access principle' : inside the foo class, that the bar field is named 'bar' is an implementation detail ; I should be able to rename it without affecting the call sites (which would still access the data through a 'bar' identifier). Moreover, that the data is accessed through a field dereference or a method dereference is also an implementation detail : the syntax should be the same in both cases (and not be bar() or getBar() as in java).

But you surely already know that and the bore that it is to write those accessors by hand... So why not have the compiler do that dirty work for us ?

should both initialize the bar field and generate the accessors for it. (but what if I only want the getter ...?)

use readonly:

class foo {
  constructor(private readonly bar: boolean) {}
}

So why not have the compiler do that dirty work for us ?

auto-implemented properties a la C# are identical to property declarations in TS. so the proposal of

class foo {
    property bar():Boolean {get;set;};
}

is

class foo {
    bar: boolean;
}

As noted by @kitsonk this is a duplicate of #7628. and as noted in #7628, JS already have an accessor declaration syntax, adding yet another form incurs a chance of conflicting syntax between TS and JS; and moreover, it is not clear that it adds much value vs. the cognitive cost of yet a new declaration form.

What about decorators?

class foo {
    @validate
    get set bar():Boolean;
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Roam-Cooper picture Roam-Cooper  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

uber5001 picture uber5001  路  3Comments

jbondc picture jbondc  路  3Comments