Releases: dphilipson/typescript-string-enums
v1.0.0
v0.3.4
Fixes sourcemaps by inlining sources.
v0.3.3
Introduces Enum.ofKeys
function. As described in the README:
Enum.ofKeys(object)
Creates a new enum with the same keys as the provided enum or object and whose values are equal to
its keys. This is most useful if for some reason it is necessary to do string comparisons against
the keys of an enum rather than the values. For example:
const ErrorColor = Enum({ OK: "green", ERROR: "red" });
type ErrorColor = Enum<typeof ErrorColor>;
const ErrorLevel = Enum.ofKeys(ErrorColor);
const errorLevel = getErrorLevel();
if (errorLevel === ErrorLevel.ERROR) {
...
}
v0.3.2
Adds Enum.isType()
, a type-checking function that can be used as a guard.
Sample usage:
const Color = Enum("RED", "GREEN", "BLUE", "PUCE");
type Color = Enum<typeof Color>;
let selectedColor: Color;
const colorString = getUserInputString(); // Unsanitized string.
if (Enum.isType(Color, colorString)) {
// Allowed because within type guard.
selectedColor = colorString;
} else {
throw new Error(`${colorString} is not a valid color`);
}
v0.3.1
Introduce Enum.keys()
and Enum.values()
, which resemble Object.keys()
and Object.values()
but provide strict typing in their return type:
const FileType = Enum({
PDF: "application/pdf",
Text: "text/plain",
JPEG: "image/jpeg",
});
type FileType = Enum<typeof FileType>;
const keys = Enum.keys(FileType);
// Inferred type: ("PDF" | "Text" | "JPEG")[]
// Return value: ["PDF", "Text", "JPEG"] (not necessarily in that order)
const values = Enum.values(FileType);
// Inferred type: ("application/pdf" | "text/plain" | "image/jpeg")[]
// Return value: ["application/pdf", "text/plain", "image/jpeg"] (not necessarily in that order)
Thanks again to @kourge for his contribution!
(This release fixes a publishing issue in 0.3.0)
v0.3.0
The artifact for this version is broken on npm. Please use 0.3.1 or later instead.
v0.2.0
Enums can now be created with keys that differ from their string values. Additionally, these entries may have JSDoc comments. For example,
export const Status = Enum({
/**
* Everything is fine.
*
* Hovering over Status.RUNNING in an IDE will show this comment.
*/
RUNNING: "running",
/**
* All is lost.
*/
STOPPED: "stopped",
});
export type Status = Enum<typeof Status>;
console.log(Status.RUNNING); // -> "running"
Thanks to @kourge for contributing!