TypeScript and Implicit Coercion
Coercion, i.e. implicitly converting a value from one type to another is considered a complex topic in JavaScript.
The reason is that there is a set of conversion rules you need to know about to predict the result of a comparision. Otherwise, some results may surprise you.
This post is about how TypeScript makes coercion easier for developers.
Here’s a set of examples from the You don’t know JS book. Try to guess the result for each operation in JavaScript (spoiler: not all of them would be false
):
Now you can check yourself with the results:
TypeScript and Coercion
A good thing about TypeScript is that it makes such comparissions easier to predict. Here’s what would be a result for the same code in TypeScript:
Some of expressions will still evaluate to false
as expected, but for most “tricky” expressions one would get the error message during the compilation (or directly in a code editor if you’re using the VS Code).
For example for the "0" == false
expression you would get: This condition will always return 'false' since the types '"0"' and 'false' have no overlap
. Other expressions would have similar error messages.
On the one hand, this limits the possibilities available for developers. On the other hand, it makes things easier to write, read, and maintain. I personally prefer the TypeScript way of supporting coercion.
Knowing coercion is still a very useful skill for any TypeScript developer. I encourage you to check the Coercion chapter of the You don’t know JS book series to refresh your knowledge on this topic.