
Security News
Static vs. Runtime Reachability: Insights from Latio’s On the Record Podcast
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
ts-key-enum
Advanced tools
A TypeScript string enum for compile-time safety when working with event.key
A TypeScript string enum for compile-time safety when working with event.key
.
npm install ts-key-enum --save
Tired of referencing JavaScript keys with a string?
onKeyPress = (ev) => {
// whoops, spelled "Enter" wrong!
if (ev.key === 'ENter') {
...
}
}
Me too. With this module, you can do this instead (in a TypeScript file):
onKeyPress = (ev) => {
// much better
if (ev.key === Key.Enter) {
...
}
}
This is similar to the ts-keycode-enum module, but it provides an enum with string values that correspond with the event.key
values instead of number values that correspond to the deprecated event.which
and event.keyCode
values.
To use this module, import the Key
enum at the top of your TypeScript file:
import { Key } from 'ts-key-enum';
You can now use the enum value in place of key strings throughout the file:
// if (ev.key === 'Escape') { ... }
if (ev.key === Key.Escape) { ... }
See Key.enum.ts for a complete list of available keys.
In addition, to aid with readability, a number of enum values have aliases. For example:
// this is true - these values are equal
Key.Hash === Key.Pound
Use whichever value makes more sense for readability.
To build this module yourself, first install its dependencies using
npm install
Then, run the build using
npm run build
Build output is placed in the dist
directory.
This module only contains some of the most commonly-used key string values. There are many more, as you can see by taking a look at this complete list. If this module is missing enum values that you need, feel free to request the additions via an issue or a pull request.
MIT
FAQs
A TypeScript string enum for compile-time safety when working with event.key
The npm package ts-key-enum receives a total of 61,872 weekly downloads. As such, ts-key-enum popularity was classified as popular.
We found that ts-key-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.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.