TypeScript Fundamentals
`any`, `unknown` & `never`
any disables type checking, unknown means a value must be checked before use, and never represents impossible values or unreachable paths.anyopts outunknownis safe uncertaintynevermeans impossible- Use
unknownat boundaries - Use
neverto make missed cases fail loudly
| Type | Meaning | Use |
|---|---|---|
| any | Compiler, stop checking this | Isolated migration or unavoidable interop |
| unknown | I do not know this yet | Untrusted input |
| never | This cannot happen | Exhaustiveness and unreachable code |
function assertNever(value: never): never {
throw new Error(`Unexpected value: ${JSON.stringify(value)}`)
}
function render(state: LoadState<User>) {
switch (state.kind) {
case "loading": return "Loading"
case "success": return state.data.name
case "error": return state.error.message
default: return assertNever(state)
}
}