
Company News
/Security News
Socket Selected for OpenAI's Cybersecurity Grant Program
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.
typescript-enum
Advanced tools
A set of missing helpers to work with TypeScript enums with runtime and compile time safety. These helpers cover both string enums and numeric enums.
npm i typescript-enum
enumValues - converts an enum to iterable list:import { enumValues } from 'typescript-enum'
enum Color { R, G, B }
console.log(enumValues(Color)) // [0, 1, 2]
isEnumValid - checks if value is a member of enumimport { enumValues } from 'typescript-enum'
enum Color { R, G, B }
const maybeColor = 0;
const notColor = 999;
console.log(isEnumValid(maybeColor, Color)); // true
console.log(isEnumValid(notColor, Color)); // false
if (isEnumValid(maybeColor)) {
// now the type of maybeColor is inferred to Color
}
❌ The following component should be updated whenever new member is added to enum
enum Permission { Read = 0, Write = 1, Edit = 2 }
const Component = () => {
return <List>
{[Permission.Read, Permission.Write, Permission.Edit].map(permission => {
// Render checkbox to edit permission
})}
</List>
}
❌ The following works only for string enums but won't work for numeric enums, because the enum gets transpiled to {"0": "Read", "1": "Write", "2": "Edit", "Read": 0, "Write": 1, "Edit": 2 }
enum Permission { Read = 0, Write = 1, Edit = 2 }
const Component = () => {
return <List>
{Object.entries(Permission).map(permission => {
// The enum values are duplicated :(
})}
</List>
}
✅ The following component doesn't need to be updated whenever new member is added to enum. Also it works with both string and numeric enums.
import { enumValues } from 'typescript-enum'
enum Permission { Read = 0, Write = 1, Edit = 2 }
const Component = () => {
return <List>
{enumValues(Permission).map(permission => {
// Render checkbox to edit permission
})}
</List>
}
❌ The following code requires unsafe type cast
enum PackageType { Basic, Advanced, Premium }
const packageInput: { type: number } = { type: 0 }
function processPackage(packageType: PackageType) {
// ...
}
processPackage(packageInput.type as any)
✅ The following code validates unknown value both runtime and compile type
import { isEnumValid } from './isEnumValid'
enum Package { Basic, Advanced, Premium }
const packageInput: { type: number } = { type: 0 }
function processPackage(packageType: PackageType) {
// ...
}
if (!isEnumValid(packageInput.type)) {
throw new Error()
}
processPackage(packageInput.type)
As you see the type inference works correctly now, the type cast is no longer needed. You can even go further and use an assertion library:
import { isEnumValid } from './isEnumValid'
import { assert } from 'ts-essentials'
enum Package { Basic, Advanced, Premium }
const packageInput: { type: number } = { type: 0 }
function processPackage(packageType: PackageType) {
// ...
}
assert(isEnumValid(packageInput.type))
processPackage(packageInput.type)
FAQs
TypeScript enum missed helpers
The npm package typescript-enum receives a total of 97 weekly downloads. As such, typescript-enum popularity was classified as not popular.
We found that typescript-enum demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.