JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q35899F
  • License MIT

Super simple type-safe fixed-length integers for TypeScript/JavaScript

Package Exports

  • typed-numbers

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (typed-numbers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

TypedNumbers

Super simple type-safe fixed-length integers for TypeScript/JavaScript

NPM VERSION NPM DOWNLOADS DEPENDENCIES

AboutInstallationUsageNaming SchemeLicense

About

I've made this library while building a little Intel 8080 emulator in JavaScript.
I noticed the lack of typed numbers in JavaScript, and how useful they could be, and then made this idea into a whole library!

Installation

With npm:
$ npm install typed-numbers

With pnpm (recommended):
$ pnpm install typed-numbers

With yarn:
$ yarn add typed-numbers

or you can directly add it to your website via unpkg:

<script src="https://unpkg.com/typed-numbers"></script>

Usage

If this project gains more attention I'll add some documentation!

import { u8, u16 } from 'typed-numbers';

// Function that takes only 16-bit unsigned integers:
function getFirstByte(num: u16): u8 {
    return u8(num & 0xf);
}

getFirstByte(127); // -> ERROR!
getFirstByte(u8(127)); // -> ERROR!
getFirstByte(u16(127)); // -> 15


// Define a 8-bit unsigned integer:
const num = u8(255);

// Add 1 (wrapped addition)
num = u8(num + 1);

// Output result
console.log(num); // -> 0

Naming scheme

The library uses the fixed-length integer naming scheme from Rust.
If you want to use another naming scheme just rename the types at import:

import { i8 as int8 } from 'typed-numbers';

or:

import { i8 as byte } from 'typed-numbers';

License

MIT License