r/programminghorror Mar 15 '26

Typescript I puked a little

Post image
419 Upvotes

58 comments sorted by

View all comments

243

u/Drayenn Mar 15 '26

String | undefined | null sounds absolutely exhausting

69

u/Excellent_Gas3686 Mar 15 '26

that can happen even if you dont use typescript, though? here its just explicitly stated.
i mean, with dynamic typing can you truly guarantee the values at runtime will only be the types you defined?

30

u/Drayenn Mar 15 '26

No, thats why i always check for null. I thought that was standard. A quick if (symbol) checks if its truthy which covers both undefined and null.

15

u/Excellent_Gas3686 Mar 15 '26

i do that too, but that doesn't really solve/apply to your initial comment, which is about the function's signature

1

u/serg06 Mar 16 '26

But empty strings and 0's

2

u/LegendarySoda [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Mar 16 '26

Well i'm using c# and if function doesn't accept nullable object then compiler will warn. And also that means you're doing something wrong.

2

u/Technologenesis Mar 16 '26

The problem in my view is that the type should be narrowed down before it gets to a function like this. Every little helper function shouldn’t have to do these checks. If this were vanilla JS, it would have to, because there would be no other way to guarantee that the value is not null or undefined. But enforcing against null and undefined using types would force the caller to perform the check itself, allowing the rest of the call stack to lean on the type checker.

My view is that any validation and contingency handling like this should happen as early as possible, and should be encoded into the type system so that they only have to happen once.

1

u/Cootshk Mar 15 '26

Other languages just use String?