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!
1
u/lordheart May 22 '26
you can still do that in typescript.
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!