Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does typescript have auto-properties yet?

Tags:

typescript

I've head that in typescript, properties can be defined the way the are in C# with auto setter and getter. Although I haven't been able to implement properties this way as the intellisense says no such syntax is supported by typescript, I couldn't find any proper example for implementing auto-properties either. All I could find about this suggested declaring methods with get and set modifiers next to them which is by no means similar to anything in C# or even automatic. So is there any auto-properties in typescript?

like image 746
Arnold Zahrneinder Avatar asked Dec 09 '17 17:12

Arnold Zahrneinder


2 Answers

No, TypeScript does not support C#-like auto properties. You have to declare getters and setters with your the usual JS syntax.

While there has been proposals for auto-properties support, none have been accepted. The gist is that this detracts from trying to move TS towards where JS should be, and is unlikely to ever be supported.

like image 114
zeh Avatar answered Sep 21 '22 06:09

zeh


Type in VSCode prop and press the "Tab" key.

Property will be generated:

export class Person
{
    private _name: string;
    public get Name(): string
    {
        return this._name;
    }
    public set Name(v: string)
    {
        this._name = v;
    }
}
like image 36
8Observer8 Avatar answered Sep 19 '22 06:09

8Observer8