Eject enums from your TypeScript codebases.
eject-enum is an automatic code rewriting tool for TypeScript codebases that rewrites each TypeScript enum in your codes to the safer alternative.
Before rewriting:
/**
* Signals of traffic light.
*/
export enum TrafficLight {
/** Stop. */
Red,
/** Stop unless you can't do so safely. */
Yellow,
/** Go. */
Green,
}
After rewriting:
/**
* Signals of traffic light.
*/
export const TrafficLight = {
/** Stop. */
Red: 0,
/** Stop unless you can't do so safely. */
Yellow: 1,
/** Go. */
Green: 2,
} as const;
export type TrafficLight = typeof TrafficLight[keyof typeof TrafficLight];
# global
npm install -g eject-enum
yarn global add eject-enum
# local
npm install --save-dev eject-enum
yarn add --dev eject-enum
# if you installed locally, prepend `npx` or `yarn`.
# rewrite all files in projects specified by TS configs.
eject-enum --project path/to/tsconfig.json path/to/tsconfig2.json
# rewrite all TS files under the `src` and `test` directories,
# except files under the `src/foo` directory.
eject-enum --include "src/**/*.ts" "test/**/*.ts" --exclude "src/foo/**/*.ts"
You can execute eject-enum from scripts as well.
/* ejectEnum.ts */
import { ejectEnum, EjectEnumTarget } from 'eject-enum';
// rewrite all files in projects specified by TS configs.
ejectEnum(EjectEnumTarget.tsConfig(["path/to/tsconfig.json", "path/to/tsconfig2.json"]));
// rewrite all TS files under the `src` and `test` directories
// except files under the `src/foo` directory.
ejectEnum(
EjectEnumTarget.paths({
include: ["src/**/*.ts", "test/**/*.ts"],
exclude: ["src/foo/**/*.ts"]
}),
);
# execute the script with ts-node
npx ts-node ejectEnum.ts
# or using esbuild-register
node -r esbuild-register ejectEnum.ts
Note
It is recommended to run code formatting tools after rewriting by eject-enum, as it doesn't consider any code formatting configurations of your project when rewriting.
if
, while
, switch
).eject-enum have some limitations about code rewriting. They originate from limitations of the TS Compiler API/ts-morph.
Generated using TypeDoc