Not using runtime assertions
TypeScript safety comes from compile-time checks when writing code. You define a type for a value and then TypeScript verifies its fair usage. This process, however, covers only 50% of the safety in general. The other 50% comes from runtime safety. For example, let's take the following function in TypeScript:
RuntimeAssertions.ts
function divMod(x: number, y: number): [number, number] { return [Math.floor(x / y), x % y]; }
This gets compiled in JavaScript as follows:
"use strict"; function divMod(x, y) { return [Math.floor(x / y), x % y]; }
Looking at the aforementioned code, you could guess that there are many potential dangers here:
- The
x
andy
parameters might not be numbers. If they are numbers encoded as strings, it will work but not if they are not a real number value:> divMod("1", 2) (2) [0, 1] > divMod("a", 2)
- Here in the second case, we passed...