2 ms·
i’m not sure about typescript, but in many languages assertion have the natural behavior of being compiled out of release builds and kept only for debug mode.
by bsaul 7y ago
i’m not sure about typescript, but in many languages assertion have the natural behavior of being compiled out of release builds and kept only for debug mode.
The fact that it’s a built in feature of the language also can sometimes helps the compiler infer what’s the properties of code after it. But theoretically , yes you could very well code some equivalent testing function yourself, except for the compiler inference part it would probably be equivalent.
- deepersprout 7y ago> i’m not sure about typescript, but in many languages assertion have the natural behavior of being compiled out of release builds and kept only for debug mode. Not in TS. This code function something(num : number) { assertIsInRange(num); return num + 1; } function assertIsInRange(num: number, msg?: string): asserts num { if (num < 50 || num > 100) { throw new Error('not in range'); } } something(50); gets compiled to function something(num) { assertIsInRange(num); return num + 1; } function assertIsInRange(num, msg) { if (num < 50 || num > 100) { throw new Error('not in range'); } } something(50);