javascript - auto getter and setter in TypeScript - Stack Overflow

I've created this (sort of auto getter and setter) for javaScript but I don't know how to imp

I've created this (sort of auto getter and setter) for javaScript but I don't know how to implement this in typeScript

I want to make an Object Oriented version of that if that's possible.

I've created this (sort of auto getter and setter) for javaScript but I don't know how to implement this in typeScript

I want to make an Object Oriented version of that if that's possible.

Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Sep 22, 2015 at 16:59 Morteza TouraniMorteza Tourani 3,5465 gold badges43 silver badges48 bronze badges 1
  • 1 Check out using decorators to do something like this. – rwisch45 Commented Sep 22, 2015 at 17:27
Add a ment  | 

2 Answers 2

Reset to default 6

7 years later, TypeScript 4.9 now supports an uping ECMAScript feature called "auto accessors":

class MyClass {
  // This property 
  accessor myProperty: boolean = true
}

This does not provide access to the underlying private property, though. You can read up on how these work and why these were introduced here.

Currently, there is no good way to do this. I think you will just need to go the boilerplate-y way:

class Foo {
    private _bar: number;
    get bar() { return this._bar }
    set bar(bar: number) { this._bar = bar}
    // ...
}

If you wanted, you could use an editor snippet to make this a bit less of a pain.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743781050a4505805.html

相关推荐

  • javascript - auto getter and setter in TypeScript - Stack Overflow

    I've created this (sort of auto getter and setter) for javaScript but I don't know how to imp

    7天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信