# safely-convert-enum

> Small TypeScript utility to convert between compatible enum values

Latest version **1.0.1** (published 2022-09-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install safely-convert-enum
pnpm add safely-convert-enum
yarn add safely-convert-enum
bun add safely-convert-enum
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-09-09 |
| First published | 2022-09-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mark Slade |
| Maintainers | mslade |
| Keywords | typescript, enum, conversion |

## Links

- npm: https://www.npmjs.com/package/safely-convert-enum
- Repository: https://github.com/maslade/ts-safely-convert-enum
- Homepage: https://github.com/maslade/ts-safely-convert-enum#readme
- Issues: https://github.com/maslade/ts-safely-convert-enum/issues
- npm.io page: https://npm.io/package/safely-convert-enum

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2022-09-09
- 1.0.0 — 2022-09-09

## README

# Safely Convert Enums Between Compatible Types
Tiny utility function that can be used to safely convert between string-based enums with identical
values.

## Usage

```ts
const sourceEnum = SourceEnum.SomeValue;
const convertedEnum: TargetEnum = safelyConvertEnum(sourceEnum);
```

## How "safe" is it?

This will produce an error in the event that `SourceEnum` and `TargetEnum` are incompatible by
value, or are not `string`-based enums.

The error it produces is unfortunately a little cryptic. Given an incompatible set of enums, the
error will present on the `sourceEnum` variable where it is passed into the function, and the error
will be similar to:

```
Argument of type '...' is not assignable to parameter of type 'never'.ts(2345)
```

## When should I use this?

You should use this only when you are dealing with two enums that are *meaningfully* equivalent.
This means both that they have the same values today, and that they (by design) should always
represent the same set of values. You should *not* use this to convert between enums that are
*coincidentally* equivalent.

---
_Source: https://npm.io/package/safely-convert-enum · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
