TypeScript Character Code Enumeration
typescript-char is a lightweight, type-definition-only library providing a TypeScript enumeration for common character codes. Its primary purpose is to eliminate 'magic numbers' in code, enhancing readability and maintainability when dealing with character-based operations, such as in parsers, lexers, or character stream processing. The library currently stands at version 0.0.0, indicating a pre-release or unversioned state. As a 'header-only' library, it consists solely of a `.d.ts` file, meaning it introduces no runtime JavaScript code or overhead. Its release cadence is effectively non-existent, with no updates since 2017. Key differentiators include its zero-runtime footprint and its focus purely on providing type-safe, named constants for character codes within a TypeScript environment, allowing direct usage of `Char.OpenBrace` instead of numerical literals like `123`.
Common errors
-
Error: Cannot find module 'typescript-char'
cause This error occurs if TypeScript cannot locate the module's declaration file, often due to incorrect `tsconfig.json` paths or if the package is not installed correctly.fixEnsure `typescript-char` is listed in your `devDependencies` and `node_modules` is accessible. Verify your `tsconfig.json` includes `node_modules` in `typeRoots` or `include` paths, and `moduleResolution` is set appropriately (e.g., `NodeNext` or `Node`). -
TypeError: Cannot read properties of undefined (reading 'OpenBrace') at runtime
cause This error arises when you try to access `Char.OpenBrace` in JavaScript at runtime after TypeScript compilation, but `typescript-char` is a type-only library and provides no runtime values.fixThis library only provides types. If you need runtime character codes, you must either define them as `const` enums (which are inlined) or use direct magic numbers in your JavaScript output, which defeats the purpose of this library for runtime values. This library is for compile-time type safety.
Warnings
- breaking The package version is 0.0.0, indicating it is pre-release and not considered stable. Future updates (if any) could introduce breaking changes without following semantic versioning practices.
- gotcha This is a 'header-only' library, meaning it provides only TypeScript type definitions (`.d.ts` file) and no runtime JavaScript code. Attempting to `require()` it in a CommonJS environment or expecting a runtime class will result in errors.
- gotcha The library has not been updated since 2017. This indicates it is likely abandoned and will not receive new features, bug fixes, or compatibility updates for newer TypeScript versions or Unicode characters.
Install
-
npm install typescript-char -
yarn add typescript-char -
pnpm add typescript-char
Imports
- Char
import { Char } from 'typescript-char'import Char from 'typescript-char'
- Char
const Char = require('typescript-char')/// <reference types="typescript-char" /> const x: Char = Char.OpenBrace
Quickstart
import Char from 'typescript-char';
// Use a character code from the enumeration
const openBraceCode: Char = Char.OpenBrace;
console.log(`Open Brace Code: ${openBraceCode}`); // Expected output: 123
// Convert the character code back to a string character
console.log(`Character from code: ${String.fromCharCode(openBraceCode)}`); // Expected output: {
// Example in a simple parser context (conceptual, as it's types only)
function processInput(input: string) {
if (input.charCodeAt(0) === Char.OpenParen) {
console.log('Input starts with an opening parenthesis.');
} else if (input.charCodeAt(0) === Char.A) {
console.log('Input starts with an 'A'.');
} else {
console.log('Input starts with another character.');
}
}
processInput('(');
processInput('Apple');
processInput('Banana');