Base functionality used throughout XY Labs TypeScript/JavaScript libraries
npm install @xylabs/enum[![logo][]](https://xylabs.com)
[![main-build][]][main-build-link]
[![npm-badge][]][npm-link]
[![npm-downloads-badge][]][npm-link]
[![jsdelivr-badge][]][jsdelivr-link]
[![npm-license-badge][]](LICENSE)
[![codacy-badge][]][codacy-link]
[![codeclimate-badge][]][codeclimate-link]
[![snyk-badge][]][snyk-link]
[![socket-badge][]][socket-link]
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
@xylabs/enum
*
- Enum
*
``ts`
function Enum
Transforms a given record object into a readonly "enum-like" structure while preserving
the literal types of its values. This allows you to use the returned object both at runtime
(for lookups) and at compile time (for strongly typed values).
To maintain literal types (i.e., prevent them from being widened to string, number, etc.),as const
ensure you annotate your object with before passing it to Enum.
T extends Record\<string \| number \| symbol, unknown\>
A record type with string keys and any kind of values.
Readonly\<T\>
A readonly record object annotated with as const.
Enum\<T\>
A readonly version of the provided record, preserving exact literal value types.
`typescript
// Defining a record with literal types using as const:
const DnsRecordType = Enum({
A: 1,
AAAA: 28,
CAA: 257,
CNAME: 5,
DNAME: 39,
MX: 15,
NS: 2,
PTR: 12,
SOA: 6,
SPF: 99,
SRV: 33,
TXT: 16,
} as const);
// DnsRecordType is now a readonly object:
// {
// readonly A: 1;
// readonly AAAA: 28;
// readonly CAA: 257;
// readonly CNAME: 5;
// readonly DNAME: 39;
// readonly MX: 15;
// readonly NS: 2;
// readonly PTR: 12;
// readonly SOA: 6;
// readonly SPF: 99;
// readonly SRV: 33;
// readonly TXT: 16;
// }
`
*
`ts`
type Enum
A utility type that, given a Record, returns a readonly versionT
of that record. This results in a type where all properties of are readonly.
T extends Readonly\<Record\<string \| number \| symbol, unknown\>\>
The record type to make readonly.
`typescript
// Given a record:
export const DnsRecordType = Enum({
A: 1,
AAAA: 28,
CAA: 257,
CNAME: 5,
DNAME: 39,
MX: 15,
NS: 2,
PTR: 12,
SOA: 6,
SPF: 99,
SRV: 33,
TXT: 16,
})
// Now the type inference will preserve the literal types:
export type DnsRecordType = Enum
`
*
`ts`
type EnumKey
A utility type that, given an Enum object, returns the union of its keys.
T extends Record\<string \| number \| symbol, unknown\>
*
`ts`
type EnumValue
A utility type that, given an Enum object, returns the union of its values.
T extends Record\<string \| number \| symbol, unknown\>
Part of sdk-js
- Arie Trouw (arietrouw.com)
- Matt Jones
- Joel Carter
- Jordan Trouw
> See the LICENSE file for license details
[logo]: https://cdn.xy.company/img/brand/XYPersistentCompany_Logo_Icon_Colored.svg
[main-build]: https://github.com/xylabs/sdk-js/actions/workflows/build.yml/badge.svg
[main-build-link]: https://github.com/xylabs/sdk-js/actions/workflows/build.yml
[npm-badge]: https://img.shields.io/npm/v/@xylabs/enum.svg
[npm-link]: https://www.npmjs.com/package/@xylabs/enum
[codacy-badge]: https://app.codacy.com/project/badge/Grade/c8e15e14f37741c18cfb47ac7245c698
[codacy-link]: https://www.codacy.com/gh/xylabs/sdk-js/dashboard?utm_source=github.com&utm_medium=referral&utm_content=xylabs/sdk-js&utm_campaign=Badge_Grade
[codeclimate-badge]: https://api.codeclimate.com/v1/badges/c5eb068f806f0b047ea7/maintainability
[codeclimate-link]: https://codeclimate.com/github/xylabs/sdk-js/maintainability
[snyk-badge]: https://snyk.io/test/github/xylabs/sdk-js/badge.svg?targetFile=package.json
[snyk-link]: https://snyk.io/test/github/xylabs/sdk-js?targetFile=package.json
[npm-downloads-badge]: https://img.shields.io/npm/dw/@xylabs/enum
[npm-license-badge]: https://img.shields.io/npm/l/@xylabs/enum
[jsdelivr-badge]: https://data.jsdelivr.com/v1/package/npm/@xylabs/enum/badge
[jsdelivr-link]: https://www.jsdelivr.com/package/npm/@xylabs/enum
[socket-badge]: https://socket.dev/api/badge/npm/package/@xylabs/enum
[socket-link]: https://socket.dev/npm/package/@xylabs/enum