Warning
Hey there, just a heads-up: I've stopped updating this repo. If you're diving into functional programming—like monads, higher kinded types, and all that fun stuff—in TypeScript, you might find some handy insights here. But a little word of caution: it's probably not the best fit for production use. Happy coding!
super-ts is a Typescript functional programming library inspired by Haskell and PureScript providing both runtime type checking and functional algebraic data types.
super-ts were built to unify concepts and tools of some wonderful libraries, such fp-ts, io-ts and runtypes.
Forwarding to be an all-in-one solution, super-ts is divided into two main categories: Algebraic Types & Runtime Types;
- Runtime Types: static and runtime type checking
- Algebraic Types: Monads, Functors, Semigroups and others.
npm install super-ts
will install super-ts for use.
You can use super-ts in two flavors: CommonJS or ES Modules.
import { String } from "super-ts/cjs/runtime";
import { Either } from "super-ts/cjs/algebraic";
import { String } from "super-ts/esm/runtime";
import { Either } from "super-ts/esm/algebraic";
PS: If you are using the package with ES Modules over node you should run your application with the flag --experimental-specifier-resolution=node
ES Modules enables tree shaking to be possible using bundlers.
super-ts provides you with primitive types that can be safely type-checked at runtime with custom constraints and use this same schema as your Typescript static type signature. You can also provide custom runtime checks to build more advanced types that fit your use cases.
Let's suppose that you have the following static type schema on your Typescript project:
type League = "NFL" | "MLB" | "NBA" | "WNBA";
type Gender = "Male" | "Female" | "Other";
type Team = {
name: string;
yearFounded: number;
league: League;
type: "team";
};
type Player = {
firstName: string;
lastName: string;
salaryOnTeam: [number, Team];
age: number;
isActive: boolean;
teamsPlayed: Team[];
gender: Gender;
type: "player";
};
This works fine on TypeScript since you use this schema to type safe your application on compile time, but what about runtime? What happens if you receive this schema as a payload from some external source?
In this scenario your application is unsafe and you need to do a lot of validations to avoid runtime errors.
To avoid all this boring work you can use super-ts runtime types to define your schema keeping your application safe on runtime as well on compile time, re-using static types generated by super-ts.
In order to define the same schema as above using super-ts we do the following:
import {
String,
Number,
Boolean,
Array,
Record,
Literal,
Tuple,
Union,
} from "super-ts/esm/runtime";
const League = Union(
Literal("NFL"),
Literal("MLB"),
Literal("NBA"),
Literal("WNBA")
);
const Gender = Union(Literal("Male"), Literal("Female"), Literal("Other"));
const Team = Record({
name: String,
yearFounded: Number,
league: League,
type: Literal("team"),
});
const Player = Record({
firstName: String,
lastName: String,
salaryOnTeam: Tuple(Number, Team),
age: Number,
isActive: Boolean,
teamsPlayed: Array(Team),
gender: Gender,
type: Literal("player"),
});
When you define your schema using super-ts you can get Typescript static types using the custom TypeOf
type.
import { TypeOf } from 'super-ts/esm/runtime'
type Player = TypeOf<typeof Player>;
// type Player = {
// firstName: string;
// lastName: string;
// salaryOnTeam: [number, {
// name: string;
// yearFounded: number;
// league: "NFL" | "MLB" | "NBA" | "WNBA";
// type: "team";
// }];
// age: number;
// isActive: boolean;
// teamsPlayed: {
// ...;
// }[];
// gender: "Male" | "Female" | "Other";
// type: "player";
// }
When you use the runtime types, we expose an API under the property .Δ
so you can use functions available for the type.
Takes an unknown payload and validates against the type. If the validation suceeds, we return an algebraic type called Resultλ of Sucess which contains the payload. If the check fails we return an Resultλ of Failure containing an NonEmptyArrayλ of InvalidCheck containing all the errors found on that payload.
Example
import { identity } from 'super-ts/common/identity';
import { Result } from 'super-ts/algebraic';
/** other imports .. */
/** above code .. */
const Team = Record({
name: String,
yearFounded: Number,
league: League,
type: Literal("team"),
});
const teamInvalidPayload = {
name: null,
yearFounded: "1974",
league: "NFL",
type: "team",
};
const isValidTeam = Team.Δ.check(teamInvalidPayload);
const isValidTeamRes = Result.λ.fold (identity, identity) (isValidTeam);
// isValidTeamRes = [
// {
// code: 'IS_STRING',
// message: 'Expected string but found (null :: object)',
// path: 'name'
// },
// {
// code: 'IS_NUMBER',
// message: 'Expected Number but found (1974 :: string)',
// path: 'yearFounded'
// }
// ]
This documentation is working in progress.. 😅 🚧