I'm used to working in functional languages with monadic constructs such as Option or Optional. I'm aware of libraries like fp-ts which offer these constructs in TS/JS. However I'm interested to know how imperative developers represent optional values in typescript.
From what I can see null or undefined can both be used along with a guard e.g.
const user = getUser(id)
if (maybeUser) {
// do something
} else {
// do something else
}
given this scenarios what is the correct return type for getUser? User | null
or User | undefined
. To me, null seems more explicit, yet undefined seems more common. I think I'm correct in saying that name?: string is a shortcut for name: string | undefined