Typescript Refactoring - Extract Constant?

One of the more common refactorings I do (in any language) is to replace values with constants. IDEA provides an "Extract Constant" refactoring fpr Java, but I don't see one for Typescript.

For example, if I've got this:

export class MyClass
{
  public foobar(): void
  {
    let foo: string = "Hello World";
  }
}

 

I'd like to be able to select "Hello World", Extract Constant, and end up with this:

 

export class MyClass
{
public static readonly HELLO_WORLD: string = "Hello World";

  public foobar(): void
  {
    let foo: string = MyClass.HELLO_WORLD;
  }
}

 

The visibility and name, of course, should be up to me. And, of course, it shouldn't be limited to strings - at the very least, it should work for numbers.

Have I missed this functionality? If not, anyone got a workaround?

0
2 comments

there is no such feature:( Please follow https://youtrack.jetbrains.com/issue/WEB-14450 for updates.

For now I'd suggest using Extract field instead - it produces the following output:

export class MyClass
{
private _foo = "Hello World";

public foobar(): void
{
let foo: string = this._foo;
}
}
1

Cool. From there, I can change the field to a public static readonly, and then do a search/replace of this._foo to MyClass._foo.

It's not great, but it's a decent workaround for now.

Thanks!

0

Please sign in to leave a comment.