r/badcode • • Jun 19 '21

typescript Check if binary tree is balanced...

Post image
221 Upvotes

44 comments sorted by

View all comments

-1

u/ef02 Jun 20 '21

The | null is unnecessary in TypeScript, but other than that, it seems fine.

3

u/MrPancholi Jun 20 '21

Why is null unnecessary? Wouldn't that cause a conflict with the type?

0

u/ef02 Jun 20 '21

Oh man...I wish I could answer any "why" question about JS/TS...but all I know is that the TS transpiler doesn't complain when I do something like that without the | null 😂

I believe that setting an object type to null is fine, but undefined / unknown would cause an error.

2

u/RedMser Jun 20 '21

Depends on whether you set the TS compiler to strict mode or not. I personally prefer having to add null or undefined to the type, so you can avoid checking for null-ish values before every function call, as long as you aren't writing library code at least.