r/ProgrammerHumor • • Sep 17 '26

Meme theMostPassiveAgressiveTypeIEverEncountered

Post image
1.4k Upvotes

83 comments sorted by

View all comments

5

u/kitaz0s_ Sep 17 '26

bad name then, Maybe<T> should be T | undefined (or null instead of undefined depending)

18

u/GRex2595 Sep 17 '26

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.

7

u/the_horse_gamer Sep 17 '26

for completeness, here's an implementation:

type Maybe<T> = {[K in keyof T]: T[K] | null}

3

u/Reashu Sep 17 '26

Right, that's why it's a bad name.

4

u/GRex2595 Sep 17 '26

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.

1

u/jordanbtucker Sep 18 '26

Maybe is a bad name because there is precedent to what that type should mean. NullableProps is better and it's not as egregious as your example.

1

u/GRex2595 Sep 18 '26

Great. You should go find whatever code base is using this name and submit a PR.

1

u/danielcw189 Sep 19 '26

because AnyPropertyCanBeSetToNull<T> is bad for different reasons.

such as?

1

u/GRex2595 Sep 19 '26

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.

2

u/garver-the-system Sep 17 '26

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?

1

u/BroBroMate Sep 17 '26 edited Sep 17 '26

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.

3

u/Wonderful-Habit-139 Sep 17 '26

There’s already another comment that explains it: https://www.reddit.com/r/ProgrammerHumor/s/ThiGS1QYv5

1

u/BroBroMate Sep 17 '26

That's not a standard TS type though, unless I'm really failing at Google

3

u/Wonderful-Habit-139 Sep 17 '26

No one said anything about it being a standard TS type. It just corrects your presumption about how it could be defined.

1

u/BroBroMate Sep 18 '26

Ah yep, I didn't understand how the Maybe impl worked precisely after Partial made every property optional.

If I understand it correctly now, if I have Foo with a type structure

type Foo {
  a: number
}

Then Partial<Foo> is

{
    a?: number
}

And then Maybe<Partial<Foo>> yields

{
    a?: number | null
}

But my confusion is, what does the Maybe even do then in this case? as the property will always return undefined unless you explicitly null it?

Or is it tied into a getOrNull style pattern like in other Maybe/Optional implementations?

2

u/jordanbtucker Sep 18 '26

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."

1

u/BroBroMate Sep 19 '26

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.

Cheers!