Hey, you got types in my JavaScript!

James Earl Douglas

May 26, 2020

Agenda

In the next ten minutes, I aim to:

Types crypt

The fundamental problem addressed by a type theory is to ensure that programs have meaning.

-- Mark Manasse

Identity crisis

What does this function do?

function foo(x) {
  // implementation hidden
}

Parametricity

What does this function do?

function foo<A>(x: A): A {
  // implementation hidden
}

Possibilities Possibility

There's only one possible implementation*: identity.

* If we ignore null, undefined, and throw.

Numeric product?

function multiply(x, y) {
  // implementation hidden
}

var z = multiply(6, 7);

What is z?

╯°□°)╯︵ ┻━┻

Numeric product...

function multiply(x: number, y: number): number {
  // implementation hidden
}

var z: number = multiply(6, 7);

Now what is z?

┬─┬ノ( º _ ºノ)

Numeric product!

interface Monoid<A> {
  unit: A;
  concat(x: A, y: A): A;
}

Testable laws: identity, associativity

\o/

It's complicated

Web browsers don't know what TypeScript is. They need JavaScript.

multiply.ts:

multiply.js:

Conclusion

Cons

Pros

References

Typescript

Software maintenance

Types and parametricity