Search code examples
typescriptvisual-studio-codeprettier

alphabetically sort TypeScript interface keys


Is there a way to automatically sort TypeScript interface keys? I'm already using Prettier and VSCode so ideally the solution would be with those but I'm open to any solutions.

For example,

// before
interface MyInterface {
  foo: number;
  bar: number;
  baz: number;
}

// after sort
interface MyInterface {
  bar: number;
  baz: number;
  foo: number;
}

Solution

  • In VS Code; you can simply select the lines you want to sort and then CMD + SHIFT + P (Ctrl + Shift + P on Windows) and search for Sort lines Ascending/Descending.

    enter image description here