Skip to content

benjlevesque/codemod-enum-to-union

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codemod for enum to union transformation

Convert Enums to Union types, as describe in https://rlee.dev/writing/stop-misusing-typescript-string-enums

  • Input
enum MyEnum {
  FOO = "foo",
  BAR = "bar2",
}
  • Output (after running prettier)
export const MyEnum = {
    FOO: "foo",
    BAR: "bar2"
} as const;
export type MyEnum = typeof MyEnum[keyof typeof MyEnum];

Usage

  1. install ts-codemod
  2. Clone this repo
  3. Make sure you have no local changes in your project's repo
  4. Run these commands
# run the transformation
ts-codemod -t ./index.ts path/to/your/project/repo/src/**/*.ts

# cleanup the code (optional)
yarn prettier --write  path/to/your/project/repo/src/**/*.ts

cd path/to/your/project/repo

# remove whitespaces difference (optional)
git diff --ignore-blank-lines | git apply --cached
git checkout -- ./

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published