vanilla JavaScript that validates function arguments properly
Validating perfectly at run time is exceedingly difficult and tedious. Does anyone actually write defensive code like this?:
function sum(arr) {
if (!Array.isArray(arr))
throw "Invalid type";
let result = 0;
for (const val of arr) {
if (typeof val != "number")
throw "Invalid type";
result += val;
}
return result;
}
By contrast, the TypeScript code is reasonable:
function sum(arr: Array<number>) {
let result: number = 0;
for (const val of arr)
result += val;
return result;
}
> it does make the code more terse
No, TypeScript code with explicit type annotations is less terse than the typical unsafe JavaScript code that lacks type checks.
It requires that you define types for every little thing
No, the TypeScript compiler has type inference. It can assign types based on how values are passed between known functions. While it's a good idea to explicitly type all function signatures in TypeScript, it's not mandatory unlike Rust.
The thing is, though, that Typescript is not solving runtime type checks. Typescript is declarative, it does not check at runtime and if you have any code interacting with third-party data sources and you do not check there, things will explode down the line.
I half agree with your response. It's true that third-party data sources need to be validated using runtime type checks, whether in explicit custom code or through some schema validator framework. But TypeScript is very helpful in the common case where the caller is fully typed, so the callee doesn't need to do all the needless defensive checking.
Comments
Validating perfectly at run time is exceedingly difficult and tedious. Does anyone actually write defensive code like this?:
By contrast, the TypeScript code is reasonable: > it does make the code more terseNo, TypeScript code with explicit type annotations is less terse than the typical unsafe JavaScript code that lacks type checks.
No, the TypeScript compiler has type inference. It can assign types based on how values are passed between known functions. While it's a good idea to explicitly type all function signatures in TypeScript, it's not mandatory unlike Rust.
The thing is, though, that Typescript is not solving runtime type checks. Typescript is declarative, it does not check at runtime and if you have any code interacting with third-party data sources and you do not check there, things will explode down the line.
You would have to check your third party data sources anyway? I fail to see how this is a knock against typescript.
Setting the type of unknown data to `unknown` forces you to validate it.
I half agree with your response. It's true that third-party data sources need to be validated using runtime type checks, whether in explicit custom code or through some schema validator framework. But TypeScript is very helpful in the common case where the caller is fully typed, so the callee doesn't need to do all the needless defensive checking.
Oddly enough, Typescript would be more useful if the type checks transpire to explicit defensive code. Otherwise, it’s just, eh.