npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

literal-as-enum

v1.0.1

Published

Converts string constants into string literal type and provides handy utility functions.

Downloads

3

Readme

Literal as enum

Converts string constants into string literal type and provides handy utility functions.

Usage

Setting up an enum type

import { GetEnumType, enumFrom } from 'literal-as-enum';

export const Status = enumFrom('idle', 'running', 'cancelling', 'terminated');
// Make `Status` is a string literal type with possible values of:
// 'idle', 'running', 'cancelling' and 'terminated'
export type Status = GetEnumType<typeof Status>;

Converting a string to an enum type

// Some dynamic input
const input = JSON.parse('"running"');

const status = Status.of(input); // status has the value of 'running'

Converting an invalid string to an enum type

// Some dynamic input
const input = JSON.parse('"hello-world"');

const status = Status.of(input); // status is undefined

Type guard

// Some dynamic input
const input = JSON.parse('"terminated"');

if (Status.validate(input)) {
  // `input` now has a type of "Status"
} else {
  // `input` is not a valid "Status"
}

Fallback

As shown in the example above, invalid string values will be converted into undefined. Should there be a need to always fallback to a default value, fallback method can be used.

const Role = enumFrom('member', 'moderator', 'admin').fallback('member');
type Role = GetEnumType<typeof Role>;

const inputA = JSON.parse('"admin"');
const roleA = Role.of(inputA); // roleA is 'admin'

const inputB = JSON.parse('"hacker"');
const roleB = Role.of(inputB); // roleB is 'member'

Quirks

As you could see in the code snippet below

const Role = enumFrom('member', 'moderator', 'admin');
type Role = GetEnumType<typeof Role>;

Line 1 creates an object (or you can think of it as an instance of a class) that comes with a bunch of useful functions to convert/validate a given value to the enum type. It is available during runtime.

Line 2 however, is inferring the initialised values in Line 1, and make the type a string literal union.

Therefore, giving both of them the same value does not conflict each other - first one is a runtime variable/instance, whilst the second is a compile-time typing. In fact, they complement each other.

However, in some ESLint configurations, no-redeclare rule was enforced, and it may complain that variable is already defined.

There are 2 (obvious) approaches of overcoming it: -

  1. Rename the variable, e.g. Role for the runtime instance and TRole for the typing.
  2. Suppress ESLint rule for the line/file, e.g. /* eslint-disable @typescript-eslint/no-redeclare */

Personally I'd go with the latter, as I find the code is cleaner. But one man's meat is another man's poison.