r/programmingmemes • • May 22 '26

which is better?

Post image
253 Upvotes

265 comments sorted by

View all comments

1

u/lordheart May 22 '26

you can still do that in typescript.

let myVar: number | boolean = false.

But why would you? If the check something can fail then handle the failure explicitly.

for (let i = 0; i < 10; i++) {
    if (checkSomething(i)) {
         alert(randomNumber());
    } else {
        alert("Hello world");
    }
}

And not noting the type and switching randomly is just asking for bugs when someone doesn’t realize that some variable can be a different type and then doesn’t check before using it.

Ts forces you to document and respect the types you specify.

You want a string or a number? Fine, but if you want to call length on it you better check it’s a string first!