I like the tricks :) – Kasia
type NonZero<Number extends number> = Number extends 0 ? "cannot be 0" : Number
const div = <Divisor extends number>(dividend: number, divisor: NonZero<Divisor>) =>
dividend / divisor
div(1, 0)
// ✅ Does not compile
// Argument of type '0' is not assignable to parameter of type '"cannot be 0"'
declare function gimmeANumber(): number
div(1, gimmeANumber())
// ⛔️ gimmeANumber() could return 0
Crafted with 💛 by Riccardo
Crafted with 💛 by Riccardo