Big Little Lies,
Big Little Types

Making React More Type Safe With Type-Tagging in Scala.js

Type-Tagging

  • Tag a type with another type
  • Augment the Slinky/React API
  • Use that other type to enforce rules at compile time
  • Make illegal states and values unrepresentable
  • Enforce order of function calls

A Bit About Me

  • Jason Pickens ( @steinybot )
  • Father of 4 (2 human)
  • Graduate at Orion Health in 2010
  • First intro to FP and Scala in 2013
  • Did a stint at Lightbend
  • Went to dinner with the Scala team
  • Now at Goodcover
  • Writing Scala professionally for ~7 years

Teaser

  • Apple
  • Banana
  • Cherries * 5

React

Untyped Children

  • Apple
  • Banana

  • Cherries * 5

Scala.js

Scala for the web

  • Now a built-in target for the Scala compiler
  • Outputs .sjsir and .class files
  • Scala.js linker produces .js files
  • Same semantics as JVM (mostly)
  • Very good JavaScript interoperability
  • Generate Scala.js facades from TypeScript (ScalablyTyped)

Slinky

Slinky

Slinky

Typed Children

Value Class





Unexpected Allocation

What About WithAttrs?

Remember KeyAddingStage?

TypedKeyAddingStage?

Type Alias





Structural Refinement



Avoiding Allocation

Extension Methods

TypedFunctionalComponent




Refined Types


Inside Refined

Unsafe Hooks

Safer Hooks

Safe...err Hooks

Stable Tag

Safety Hooks

Safest Hooks

Type-Tagging

  • Tag a type with another type
  • Use that other type to enforce rules at compile time
  • Augment an existing API
  • Make illegal states and values unrepresentable
  • Enforce order of function calls
  • Fewer bugs
  • More maintainable code

Type-Tagging

Thanks!

Slides and code: steinybot/big-little-types

Libraries coming soon...