Write type-safe, extensible JavaScript without a compiler
npm i @fed1/jsos git clone https://git.fed1.org/jsos.git
Open Source Licensed under MIT-0 (public domain license)
Powerful Features Multimethods, Traits, Structs, and Contracts
Zero Dependencies Written from scratch in vanilla JavaScript
Small Footprint Only 30 kb minified core, 41 kb full bundle
Inspired By The Best Julia, Common Lisp, and Rust

JS Object System (JSOS)

JSOS is an open-source multiple dispatch library and type system for JavaScript.

It features multimethods akin to Common Lisp, a type system inspired by Julia, and interface enforcement using contracts and Rust-like traits.

Asteroids example

Compare with: Julia, @arrows/multimethod

import { Generic, def, Type, Struct, JSNumber } from "@fed1/jsos";

const { TSpaceObject } = Type;
const { Asteroid }     = Struct({size: JSNumber})(TSpaceObject);
const { Spaceship }    = Struct({size: JSNumber})(TSpaceObject);
const BigObject        = TSpaceObject.where(s => s.size > 100);

const { collide } = Generic;
def(collide, [BigObject, BigObject],       (a, b) => "Big boom!");
def(collide, [TSpaceObject, TSpaceObject], (a, b) => collideWith(a, b));

const { collideWith } = Generic;
def(collideWith, [Asteroid, Asteroid],   (a, b) => "asteroid/asteroid");
def(collideWith, [Asteroid, Spaceship],  (a, b) => "asteroid/ship");
def(collideWith, [Spaceship, Asteroid],  (a, b) => "ship/asteroid");
def(collideWith, [Spaceship, Spaceship], (a, b) => "ship/ship");
> collide(Asteroid.new({size: 101}), Spaceship.new({size: 300}))
'Big boom!'
> collide(Asteroid.new({size: 10}), Spaceship.new({size: 10}))
'asteroid/ship'
> collide(Spaceship.new({size: 101}), Spaceship.new({size: 10}))
'ship/ship'

Headache-free Extensibility

Software written using JSOS can be extended without touching any of the already existing code. This makes JSOS a perfect choice when you want to develop a base version of your software that can be customized for a specific subset of your customers.

See: How to build an extensible architecture with JSOS

Type Smarter, Not Harder

When we created JSOS, we wanted to build a type system that works for you, not against you.

What we came up with is a system of reified types (aka "first-class" types) that can be passed around at runtime like any other JavaScript object.

This means there's no compiler and no build steps. You can plug the type system into your software like any other library.

See: JSOS vs. TypeScript, The JSOS Type System

Better Aggregate Types

  • Structs allow you to define your own composite types
  • Dicts are like maps with typed properties and values
  • Vectors are like arrays with typed items
  • Enums are like smart constants

JSOS's data structures come in immutable and mutable versions, the latter being observable out of the box.

Ready to look at JSOS a bit closer? Why not check out our examples?

Or maybe you would like to compare JSOS to other languages and libraries.