nicoespeon / abracadabra

Automated refactorings for VS Code (JS & TS) ✨ It's magic ✨
https://marketplace.visualstudio.com/items?itemName=nicoespeon.abracadabra
MIT License
809 stars 48 forks source link

New refactoring (TS): Convert Omit<T> to Pick<T> and vice versa #769

Open automatensalat opened 1 year ago

automatensalat commented 1 year ago

Is this request related to a problem? Please describe.

In Typescript there's a way to declare a type as a subset of another types properties, using Pick and Omit. Pick explicitly includes the properties of the source type while Omit excludes them and includes the others that were not specified.

type MyType = {
  a: number;
  b: number;
  c: number;
}

// the refactoring would convert between those two representations:
type OnlyAB1 = Pick<MyType, "a" | "b">; 
type OnlyAB2 = Omit<MyType, "c">;

Sometimes over time it turns out that a type definition using Pick would be better written using Omit, or the other way around.

Describe the solution you'd like

I'd like to have a refactoring that works on the TS level which converts one declaration to the other. E.g. if a type is declared using Pick and I execute the new Convert to Omit<> refactoring, it would analyze the source type, find out which properties were implicitly excluded using the previous Pick notation and make that explicit with the Omit notation.

nicoespeon commented 1 year ago

Love the idea!

I think we still struggle to determine types that are imported (at least, we can't do that in Action Providers, too heavy). But it would work just fine for a locally defined type I think.