Skip to content

Commit daa52c6

Browse files
committed
Updated the README with an animated example.
1 parent 072ad37 commit daa52c6

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

README.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
# Typed Binary
2-
<a href="https://twitter.com/ivesiris" rel="nofollow"><img src="https://img.shields.io/badge/created%[email protected]" alt="Created by Iwo Plaza"></a>
2+
3+
<a href="https://github.com/iwoplaza" rel="nofollow"><img src="https://img.shields.io/badge/created%[email protected]" alt="Created by Iwo Plaza"></a>
34
<a href="https://opensource.org/licenses/MIT" rel="nofollow"><img src="https://img.shields.io/github/license/iwoplaza/typed-binary" alt="License"></a>
45
<a href="https://www.npmjs.com/package/typed-binary" rel="nofollow"><img src="https://img.shields.io/npm/dw/typed-binary.svg" alt="npm"></a>
56
<a href="https://www.npmjs.com/package/typed-binary" rel="nofollow"><img src="https://img.shields.io/github/stars/iwoplaza/typed-binary" alt="stars"></a>
67

78
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.
89

10+
## Prioritising Developer Experience
11+
Serialise and deserialise typed schemas without the need for redundant interfaces or an external DSL. Schemas themselves define what type they encode and decode, and **the IDE knows it**!
12+
13+
![Basic Type and Documentation Inferrence](/docs/media/basic-type-and-doc-inferrence.gif)
14+
15+
Above is a self-contained code snippet using typed-binary. The IDE can properly infer what `Dog` is. What's even more interesting, is that the "parsed" properties inherit the schema's **JSDocs** (seen on the gif above).
16+
17+
## Highlight feature
18+
The feature I am most proud of would have to be [recursive types](#recursive-types). I wasn't sure it it would be possible to achieve without additional tooling, but pushing the TypeScript type inference engine to it's extremes paid off.
19+
920
# Table of contents
1021
- [Features](#features)
1122
- [Installation](#installation)
@@ -22,12 +33,14 @@ Gives tools to describe binary structures with full TypeScript support. Encodes
2233
- [Serialization and Deserialization](#serialization-and-deserialization)
2334

2435
# Features:
25-
- Type-safe schemas (your IDE will know what structure the parsed binary is in).
26-
- Generic objects
27-
- Estimating the size of any resulting binary object (helpful for creating buffered storage)
36+
- [Type-safe schema definition system](#defining-schemas) (your IDE will know what structure the parsed binary is in).
37+
- [JSDoc inheritance](#prioritising-developer-experience)
38+
- [Estimating the size of any resulting binary object (helpful for creating buffered storage)](#serialization-and-deserialization)
2839

2940
### Why Typed Binary over other libraries?
3041
- It's one of the few libraries (if not the only one) with fully staticly-typed binary schemas.
42+
- Since value types are inferred from the schemas themselves, there is a **single source-of-truth**.
43+
- No external DSL necessary to define the schemas, meaning you have instant feedback without the need to compile the interface definitions.
3144
- It has **zero-dependencies**.
3245
- It's platform independent (use it in Node.js as well as in in Browsers)
3346
- While being made with TypeScript in mind, it also works with plain JavaScript.
@@ -43,7 +56,11 @@ To properly enable type inference, **TypeScript 4.5** and up is required because
4356

4457
# Basic usage
4558
```ts
46-
import { object, arrayOf, INT, STRING, BOOL, Parsed } from 'typed-binary';
59+
import {
60+
Parsed,
61+
object, arrayOf,
62+
INT, STRING, BOOL,
63+
} from 'typed-binary';
4764

4865
const GameState = object({
4966
nickname: STRING, // Variable-length string
@@ -56,7 +73,7 @@ const GameState = object({
5673
}),
5774
});
5875

59-
// Automatically generating the parsed type.
76+
// Type alias for ease-of-use
6077
type GameState = Parsed<typeof GameState>;
6178

6279
//...
@@ -77,7 +94,7 @@ async function loadGameState(): Promise<GameState> {
7794
catch (e) {
7895
// Returning the default state if no saved state found.
7996
return {
80-
nickname: "Default",
97+
nickname: 'Default',
8198
stage: 1,
8299
newGamePlus: false,
83100
collectables: [],
391 KB
Loading

examples/inferredShowcase/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { object, STRING, INT, Parsed } from 'typed-binary';
2+
3+
// Describing the Dog schema.
4+
const Dog = object({
5+
/** The name of the doggy. */
6+
name: STRING,
7+
/** The dog's age in dog years. */
8+
age: INT,
9+
});
10+
11+
// Creating a type-alias for ease-of-use.
12+
type Dog = Parsed<typeof Dog>;
13+
14+
// Creating a 'Dog' object.
15+
const dog: Dog = {
16+
name: 'Sammy',
17+
age: 15,
18+
};

examples/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"example:genericTypes": "ts-node genericTypes/index.ts",
88
"example:genericEnumTypes": "ts-node genericEnumTypes/index.ts",
99
"example:stateMachine": "ts-node stateMachine/index.ts",
10-
"example:customSchema": "ts-node customSchema/index.ts"
10+
"example:customSchema": "ts-node customSchema/index.ts",
11+
"example:inferredShowcase": "ts-node inferredShowcase/index.ts"
1112
},
1213
"devDependencies": {
1314
"ts-node": "^10.4.0",

0 commit comments

Comments
 (0)