Skip to content

Getting Started

Gives tools to describe binary structures with full TypeScript support. Encodes and decodes into pure JavaScript objects, while giving type context for the parsed data.

We recommend importing the library as a namespace (import * as bin from 'typed-binary'), which is what the guides use. Bundlers only include the parts of the library you use either way, so named imports (import { struct, u32 } from 'typed-binary') work just as well.

npm install --save typed-binary
import * as bin from 'typed-binary';
// Define a schema
const Vec2f = bin.tuple([bin.f32, bin.f32]);
const Vec2fSize = Vec2f.measure(bin.MaxValue);
const buffer = new ArrayBuffer(Vec2fSize);
Vec2f.write(bin.cursor(buffer), [0.5, 3.14]);
// Infer the type of values the schema reads
type Vec2f = bin.ExtractOut<typeof Vec2f>; // [number, number]
import * as bin from 'typed-binary';
// Define a schema
const Vec2f = bin.tuple([bin.f32, bin.f32]);
const Vec2fSize = Vec2f.measure(bin.MaxValue);
const buffer = Buffer.alloc(Vec2fSize);
Vec2f.write(bin.cursor(buffer), [0.5, 3.14]);