Ectype — Js is all you need

Volodymyr Pavlyshyn
3 min readOct 11, 2023

How to bring types and rusty flavor to a vanilla js

We have all struggled with a JS for almost 3 decades. Big Dream is a better language with types and nice features but we are all locked in a backward-compatible trap.

Fists and quite common idea was to create a new language with all cool features that trapspiled to JS. Coffescript was a pioneer that created a movement that transformed Javascript into ES6 language.

The continuation for this concept was a babel and now it is a Typescript and Rescript. This approach has a few downsides

  • Overhead of transpiration
  • need to learn new language and new syntax
  • as side effect new and complex tooling and infrastructure
  • editors
  • compilers
  • bundlers and source maps
  • challenge with debugging and diagnostics

Ectyps take an elegant and different approach. So we have 2 kind of problems:

  • some behavior that we like to add to a language
  • types
  • variant and sum types
  • dependent and conditional types
  • some behavior that we want to avoid or disallow
  • scoping
  • type coercion
  • broken equality

Additive behavior could be added in a lisp or racket style with a flavor of language-oriented programming. So we add a language features as a library and set of functions. unfortunately javascript is not lisp or Racket so it means that language designers couldn’t add a syntax or change a syntax. This means that we pay the extra cost with more verbose language. Substractive behavior could be added via linters and preprocessors. So we lint code before and runtime keeping in mind that the linter made his part of the work https://gitlab.com/dejawu/ectype is a combination of type library and a linter that works together. The biggest beauty — it is all native and vanilla Javascript.

Data Types

All data types are set of primitive types

  • Null — you could avoid null but you need it for backward compatibility
  • Bool
  • Num
  • Str

Complex Types

  • struct
  • array
  • fn
  • tuple
  • variant

Variants — make Js rusty

one of the biggest features — we could make javascript more rusty with sum types and even introduce pattern matching.

One more side effect of subtypes — now you could avoid a NULL and undefined and all this horrible multimillion mistakes in a code with Rust like Option and Result type

const MaybeStr = variant({
Some: Str,
None: Null,
});
const someStr = MaybeStr.from({ Some: "abc" });// Type-checking should fail when a handler is missing.
someStr.match({
Some: fn([Str], Null).from((s) => {
return null;
}),
});

Conditional Types

It is a minimal implementation of dependent and value aware types that give you the possibility to lift logic and constraint checks to a type itself

const Email = cond(Str, (val) => {
return val.includes("@");
});

Static and runtime checks

It is reusable and common way to describe and use a type information for static and runtime checks

To learn more watch a talk and try to play with projects

--

--

Volodymyr Pavlyshyn

I believe in SSI, web5 web3 and democratized open data.I make all magic happens! dream & make ideas real, read poetry, write code, cook, do mate, and love.