Giter Club home page Giter Club logo

enum2union's Introduction

enum2union

Convert Enums to Union types

Example

  • Input:
enum Direction {
    Up = 'UP', 
    Down = 'DOWN',
}
  • Output without helperDir option:
export const Direction = {
    Up: "UP",
    Down: "DOWN",
} as const;
export type Direction = (typeof Direction)[keyof typeof Direction];
  • Output with helperDir option:

object-values.ts

export type ObjectValues<T> = T[keyof T];
import { ObjectValues } from "./types/object-values";
export const Direction = {
    Up: "UP",
    Down: "DOWN",
} as const;
export type Direction = ObjectValues<typeof Direction>;

Usage

CLI

tsx ${pathToLibrary}/enum2union/src/index.ts --project-files='./src/**/*.{tsx,ts}' --helper-dir='src/types/helpers'

API

import { transform } from './src/index.ts';

transform({
  projectFiles: './src/**/*.{tsx,ts}',
  helperDir: './src/types',
});

Why not to use enums?

Typescript has unions, so are enums redundant? - Stack Overflow

Enums considered harmful - YouTube

Enums are MISUNDERSTOOD (not terrible): Josh Goldberg, typescript-eslint maintainer - YouTube

https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums

Alternatives to enums in TypeScript

Related Projects

benjlevesque/codemod-enum-to-union doesn't have ObjectValues helper, the code is formatted worse

ESLint

cartant/eslint-plugin-etc: More general-purpose (TypeScript-related) ESLint rules

shian15810/eslint-plugin-typescript-enum: ESLint rules for TypeScript enums.

https://typescript-eslint.io/linting/troubleshooting/#how-can-i-ban-specific-language-feature

TODO

  • transform string enum to union
  • add helper ObjectValues
  • insert import of helper after all imports
  • fix insertStatements dsherret/ts-morph#1192
  • improve formatting by adding space after type and const declaration
  • support import alias
  • use ts for resolving import microsoft/TypeScript#50187
  • publish as package

enum2union's People

Contributors

andriyor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.