Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate Getter and Setter for Typescript in VSCode

I'm used to using webstorm to generate getter and setter for classes.

Is there any way to do it with VSCode in short steps?

like image 498
Daniel Delgado Avatar asked Jun 21 '18 03:06

Daniel Delgado


People also ask

Can we use getter and setter in TypeScript?

In TypeScript, there are two supported methods getter and setter to access and set the class members.

How do I automatically set getters and setters in Visual Studio?

You just simply press Alt + Ins in Android Studio. After declaring variables, you will get the getters and setters in the generated code.

What is getter and setter in TypeScript?

Introduction to TypeScript Getters and Setters The getters and setters allow you to control the access to the properties of a class. For each property: A getter method returns the value of the property's value. A getter is also called an accessor. A setter method updates the property's value.


3 Answers

This feature is shipped builtin with Visual Studio Code 1.24 (May 2018); see here: https://code.visualstudio.com/updates/v1_24#_generate-gettersetter-refactoring

Basically you select the line with the property, then a Quick Action bulb is shown which you have to click. Then you can generate the getters/setters.

like image 99
John Archer Avatar answered Oct 16 '22 15:10

John Archer


The shortcut is Ctrl+Shift+R after highlighting the property.

Longer method: Right click on the property. Then click on refactor from the popup menu.

Currently, vscode has no feature to mass generate getters and setters for all class properties at once like in other IDEs so it has to be done one property at a time.

like image 30
Olofu Mark Avatar answered Oct 16 '22 16:10

Olofu Mark


A VS Code plugin that works to create Java style getters and setters (accessor and mutator methods) is Wilson Godoi's TypeScript's Getters and Setters Object Oriented Programming Style.

https://marketplace.visualstudio.com/items?itemName=Wilson-Godoi.wg-getters-and-setters

enter image description here

like image 13
Jason Avatar answered Oct 16 '22 15:10

Jason