Daniel Delgado
Daniel Delgado

Reputation: 5323

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?

Upvotes: 33

Views: 102978

Answers (7)

Ngô Anh
Ngô Anh

Reputation: 1

Right click > Source Action... > Generate Getter and Setter

Upvotes: 0

Jason
Jason

Reputation: 2830

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

Upvotes: 15

Uday Chauhan
Uday Chauhan

Reputation: 1148

To generate getter and setters in typescript :

1)Download and install the TypeScript Toolbox extension in vscode. https://marketplace.visualstudio.com/items?itemName=DSKWRK.vscode-generate-getter-setter?

2)Press Ctrl+Shift+X or Cmd+Shift+X to open the Extensions viewlet. Find the Go extension, click on it to open the Extension Editor.

3) You can view the Visual Studio Code settings for the Go extension along with their default values and description in the product itself. enter image description here

4) tick the option for Use 'getValue' and 'setValue' instead of 'get value' and 'set value'.

enter image description here

How to use :

1) Press Ctrl+Shift+P or Cmd+Shift+P

2) Enter and search for command Generate All Getter and Setter then hit Enter.

enter image description here

Upvotes: 0

Olofu Mark
Olofu Mark

Reputation: 1040

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.

Upvotes: 26

Palash Toshniwal
Palash Toshniwal

Reputation: 99

write prop in service file or any .ts file and hit enter!

Upvotes: 2

John Archer
John Archer

Reputation: 2515

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.

Upvotes: 47

user6749601
user6749601

Reputation:

May someone correct me who knows it better, but as far as I know the bare VS Code is not capable of providing this convenience. I'm sure that there exist certain plugins you can use, but I don't know any name.

Edit

Maybe TypeScript Toolbox is what you are searching for.

Upvotes: 0

Related Questions