Butter EnumsTypesafe enums as smooth as butter
Tuples, objects, and more
Tuples, objects, and more
npm install butter-enumsimport { ButterKeyedEnum, ButterTupleEnum } from "butter-enums"import { ButterTupleEnum } from "butter-enums"
const test = ButterTupleEnum(['blue', 'green', 'red'])
console.log(test.tuple)
console.log(test.enum)
import { ButterKeyedEnum } from "butter-enums"
const test = ButterKeyedEnum({
green: {
emoji: '🟩',
hex: '#00FF00',
},
}, {
// This is optional, but if you want tuple support or ordered keys, you must provide it.
// Typescript cannot convert from a union to a tuple with
// * Guaranteed order
// * Better performance than O(n^2)
// See https://stackoverflow.com/questions/55127004/how-to-transform-union-type-to-tuple-type
//
// So, we have to provide a tuple factory. It constrains the tuple to make sure you're not missing any values.
// Making our typescript compiler happy.
tupleFactory: (enumObject) => [
enumObject.green,
]
})
console.log(test.enum.green)