According to the post, Maybe<T> allows you to set properties of T to null. T | null will allow the object as defined in its template or null. Any non-null properties would not be allowed to be null.
Sometimes we deal with Maybe<T>s because AnyPropertyCanBeSetToNull<T> is bad for different reasons. I don't judge too hard when I see something that doesn't have a good name and went with more or less the least bad one they could come up with.
Mostly it's just a massive pain. It takes up a bunch of space and makes it harder to read the code as a result. If you can avoid long names, that's generally better. I couldn't imagine an AnyPropertyCanBeSetToNull<Partial<ConfigurationLoader>> testDefaultUserConfigurationLoader in my codebase, and I'll probably ask somebody who submits that in a PR to come up with anything else.
But isn't that different? I'm coming from a Python background so I may be off base here, but it sounds like Partial lets you pick some fields while dropping others. Almost like the loop started with for attr in kept_attrs: new_obj.set_attr(arg_obj.get_attr)
Or am I asking for logic from the language that famously has none?
I was going to say Partial is like Pick and Omit that lets you basically create anonymous structural subtypes in a type signature, and then I got confused and now I'm reading docs and I'm even more confused, but let's just say, there's a reason some person was able to implement a Sudoku solver entirely in TS types.
Oh okay, basically Partial<T> means that every property of T is now optional. So if T.foo is a property of T, well now it can be legitimately undefined and no type checker complaints - although I imagine linters fucking hate Partial, for obvious reasons.
Maybe<T> doesn't appear to be a built-in TS type that I can find in the docs, so it's from a 3rd party library or some home rolled horror, but presumably it's the typical Maybe<T> = Some<T> | None result type pattern.
Maybe could be used to type input where you're unsure if null could be returned, and considering the name, I feel like that's what it was made for.
"Maybe this property is null, maybe it's not. The developer must check this property against null/undefined before using it because we can't be sure it's non-null."
And doing some reading, you can use the "truthiness" of both so you could go if (Foo.a) {} (assuming you're okay with handling 0 as false also) then undefined or null, don't matter, it's false.
View all comments
5
u/kitaz0s_ Sep 17 '26
bad name then,
Maybe<T>should beT | undefined(ornullinstead ofundefineddepending)