any vs unknown vs never
Everything Relay reads from outside its own code (the environment, relay.json, a webhook body)
is data the compiler has never seen. TypeScript has two types for “I don’t know what this is”, and
they behave in opposite ways. One turns the checker off, and the other makes you prove each
assumption. A third type, never, describes what can’t exist at all.
any: the checker is off
Section titled “any: the checker is off”Every operation on any is allowed, and any spreads into everything it touches:
const data: any = JSON.parse(`{ "port": "3000" }`)
const port: number = data.port // accepted: it's really the string "3000"data.queue.host.toUpperCase() // accepted: crashes at runtimeconst doubled = data.port * 2 // doubled is any tooJSON.parse, response.json() and older libraries return any. Each one is a hole in the type
system, and everything computed from it inherits the hole.
unknown: prove it first
Section titled “unknown: prove it first”unknown also accepts any value, but allows nothing until you narrow it:
const data: unknown = JSON.parse(`{ "port": "3000" }`)
// @ts-expect-error: 'data' is of type 'unknown'data.port
if (typeof data === "object" && data !== null && "port" in data) { const port = data.port // unknown: still has to be checked if (typeof port === "number") { port.toFixed(0) // number }}The rule for data from outside: type it unknown at the boundary, narrow it once into a
precise type, and use only the precise type after that. That’s what validate does in the
exercise. In M4 you’ll generate the precise type from the validator itself.
Narrowing unknown to an object
Section titled “Narrowing unknown to an object”typeof x === "object" accepts null and arrays, so a reusable predicate is worth writing once:
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null && !Array.isArray(value)
const isStringArray = (value: unknown): value is string[] => Array.isArray(value) && value.every((item) => typeof item === "string")never: no value at all
Section titled “never: no value at all”never is the empty type. A function that always throws returns never, and a variable that
every case has been ruled out of is never:
function fail(message: string): never { throw new Error(message)}
type LogLevel = "debug" | "info" | "warn" | "error"
function rank(level: LogLevel): number { switch (level) { case "debug": return 10 case "info": return 20 case "warn": return 30 case "error": return 40 default: { const unreachable: never = level // only compiles if every case is handled return fail(`unknown level: ${String(unreachable)}`) } }}Add a fifth level and the default branch stops compiling, pointing at the function that needs a
new case. M2 builds on this exhaustiveness check.
never also appears when something went wrong: a conflicting intersection (lesson 5), or a union
narrowed until nothing is left. An unexpected never in a hover usually means two types disagree.
The type hierarchy
Section titled “The type hierarchy”unknownis the top type: every value is assignable to it.neveris the bottom type: it’s assignable to every type, and nothing is assignable to it.anyis both and neither: it opts out of the hierarchy.
Exercise
Section titled “Exercise”validate takes any and returns it as a FileConfig. Make it take unknown (the hidden tests
check), narrow step by step, and reject everything the rules describe.
anyturns the checker off and spreads. Don’t let it past the line where data enters.unknownaccepts anything and allows nothing until you narrow it. Use it for all outside data.- Narrow
unknownonce, at the boundary, into a precise type. neverhas no values: functions that always throw, and the exhaustiveness check.
Further reading
Section titled “Further reading”- Handbook: Everyday Types:
anyand type assertions. - Handbook: Narrowing: narrowing
unknownbefore use.