You should use the Nullish type

type Nullable<T> = T | null
type Nullish<T> = T | null | undefined
const getNumber = (n: Nullish<number>) => n ?? 0
const getNumber = (n: Nullish<number>): number => {
return match(n)
.with(__.nullish, () => 0) // nullish case
.otherwise((v) => v);
};
const getNumber = (n: Nullish<number>): number => {
return match(n)
.when((v): v is number => v != null, (v) => v)
.otherwise(() => 0); // nullish case
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rob Bertram

Rob Bertram

“A language that doesn’t affect the way you think about programming is not worth knowing.”