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];
- install ts-codemod
- Clone this repo
- Make sure you have no local changes in your project's repo
- 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 -- ./