WhoBuilt

Statically Enforcing A Consistent UI Using Type Systems

Oct 9, 2019· 24 min· 416 views·Watch on YouTube

Speaker

Jordan Brown, who works on Facebook’s Flow team on its React and object models, explains how Flow and TypeScript can encode UI rules at React Native EU 2019 in Poland.

Why types help teams

Types catch issues such as null values and incompatible data, reducing the mental load on authors and reviewers. They also document function inputs and outputs, flag breaking changes at call sites, and help IDEs offer useful suggestions.

Encode invariants in types

Brown defines an invariant as a property a program should always preserve, such as a list never being empty. He uses a Flow opaque type to hide a nonempty list’s pair-based representation, so users of the type cannot alter it in ways that break that guarantee.

Restrict component props

React component props are one place to enforce UI rules. Brown’s example replaces an unrestricted color string with a set of approved colors, so an off-theme value produces a type error.

Control how components compose

A component’s return type can restrict what it renders, while a menu’s children can be limited to menu items and separators. Brown then relaxes the rule to allow wrapper components that recursively render those approved elements, such as a disabled menu item.

Adopt types gradually

Brown recaps that types can make interfaces more consistent as well as catch bugs. He says Flow and TypeScript can both be adopted gradually in large codebases, and recommends researching which fits a team’s needs.

Takeaways

  • Model important UI rules as types so violations surface during development.
  • Use opaque types to protect data structures whose invariants callers should not be able to break.
  • Limit component props to approved design values, such as a defined color set.
  • Constrain component children to valid combinations, and account for wrappers that preserve those combinations.
  • Adopt Flow or TypeScript incrementally rather than waiting for a full codebase conversion.

Mentioned in this talk

Search inside this talk

What was claimed 6

  • role at
    Jordan Brown is Flow team member working on the React and object models at Meta
    “Specifically, I work on our React model and our object model.”
    YouTube
    Unverified1 source· Talk
  • works at
    “I work on the Flow team at Facebook.”
    YouTube
    Unverified1 source· Talk
  • feature
    type systems: Can flag breaking changes reflected in dependency types
    “Any breaking changes reflected at the type level ... would be pointed out automatically by the type system.”
    YouTube
    Unverified1 source· Talk
  • feature
    flow: Supports opaque types that hide their underlying structure outside the defining file
    “In Flow, that means the structure of the underlying type is visible only in the file that defines it.”
    YouTube
    Unverified1 source· Talk
  • feature
    type systems: Can encode invariants in React component props and constrain rendered component composition
    “there are two important steps: first, find the information you want to protect, and then make restrictions on it.”
    YouTube
    Unverified1 source· Talk
  • uses
    “This pattern is being used a lot in the Facebook rewrite announced at F8 this year.”
    YouTube
    Unverified1 source· Talk

Related talks