202208112226 Tests are not a substitute for types
Tests cannot and should not be viewed as a replacement for a type system. Imagine needing to test all function arguments against all permutations of possible variable types in a dynamic language. It's certainly possible, but it's not done in practice and would bloat your test suite immensely. Instead tests should be used to verify the code under test is logically correct and produces the intended outcome.
Tests can never provide an unbroken chain of checks, from the database all the way to the frontend React props, guaranteeing that all of the data has the right shape. Types can't (usually) tell us when we accidentally put a "!" in front of a conditional [emphasis mine]. Focusing on one or the other means sacrificing quality, work efficiency, or both.1
The constraints, invariants, and information encoded in type systems also serve more purposes than just ensuring things like correct usage. They also serve as documentation and communication mechanisms about intent.
Conversely 202208112228 Types are not a substitute for tests. These two systems need to be used together to achieve the best outcome.
-
Bernhardt, G. (2020, April 13). Execute Program. https://www.executeprogram.com/blog/are-tests-necessary-in-typescript ↩