Skip to content

Instantly share code, notes, and snippets.

@jamiebuilds
jamiebuilds / tradeoffs-in-value-derived-types-in-typescript.md
Last active December 16, 2022 17:21
Value-derived types in TypeScript are super powerful, but you should be thoughtful in how/when you use them

Tradeoffs in value-derived types in TypeScript

Many of the more "advanced" typescript features can be used for creating "value-derived" types.

At its simplest form:

let vehicle = { name: "Van", wheels: 4 }
declare interface CSS {
layoutWorklet: Worklet // eslint-disable-line no-undef
paintWorklet: Worklet // eslint-disable-line no-undef
animationWorklet: Worklet // eslint-disable-line no-undef
}
declare type ChildDisplay = "block" | "normal"
declare type LayoutSizingMode = "block-like" | "manual"
declare interface LayoutOptions {

assert() (sometimes called invariant())

Instead of checks like:

if (value === null) {
  throw new Error("missing value")
}
doSomethingThatNeedsValue(value)
async function validatePassword(password: string): string[] {
let errors = []
// 1. Don't regex for things you can trivially express in code
// -----------------------------------------------------------
// For example, we could have written this as `/^.{0,7}$/` but that's not
// nearly as clear as checking the length of the string.
if (password.length < 8) {
errors.push("Password must be at least 8 characters long")
// Builds array of everything ahead of time
function collectAllItems() {
return [calculateFirst(), calculateSecond(), ...]
}
// This loop will end as soon as `isMatch(item)` is truthy.
// If the very first item in the array is a match, then we
// wasted all this time building the array in the first place.
for (let item of collectAllItems()) {
if (isMatch(item)) {

Today is the start of Pride Month. I know this is not how many of you wanted to spend this month. But it's important that we remember that Black people have been an inseparable part of Pride since the very beginning. And now is the time for solidarity.

In recent years, Pride has seen a lot of gentrification. Pride has become whiter, straighter, more corporate, and importantly: more policed. In effort to make some people more comfortable, the enormous presence of cops at Pride has made many others feel unwelcome and unsafe. Disproportionately affecting the Black members of our community.

For the white members of Pride: Solidarity means making yourself uncomfortable. White Guilt and White Fragility often mean we as white people defer to authority figures to fix the problems Black people face. But many of the problems are in those authority figures, and they have shown they will not fix themselves.

This Pride we are proud of the Black history of Pride. We are proud of the Black members of our community.

Blac

let INDENT = { op: "indent" }
let OUTDENT = { op: "outdent" }
let NEWLINE = { op: "newline" }
let SPACE = { op: "space" }
function* printNode(node) {
if (node.type === "Program") {
for (let item of node.body) {
yield* printNode(item)
yield NEWLINE
class Tree {
constructor() {
this.children = []
}
add(value, parentValue) {
let newNode = { value, children: [] }
if (parentValue === null) {
this.children.push(newNode)