Today I learned: tslib is not a devDependency
About 1 min reading time
There are some dependencies that I automatically add as devDependency
if they are needed. This includes tslib, since it obviously belongs to TypeScript and therefore should not play a role at runtime.
That's where I was wrong. As my colleague Paul and I found out yesterday, you need tslib
at runtime if you set importHelpers
to true
in tsconfig
. Since this avoids duplicate code, it should be worth adding an additional dependency, especially for large projects.
For certain downleveling operations, TypeScript uses some helper code for operations like extending class, spreading arrays or objects, and async operations. By default, these helpers are inserted into files which use them. This can result in code duplication if the same helper is used in many different modules.
If the importHelpers flag is on, these helper functions are instead imported from the tslib module. You will need to ensure that the tslib module is able to be imported at runtime. This only affects modules; global script files will not attempt to import modules.
By the way, the documentation at GitHub gets it right. I should have taken a closer look there, for sure. Unfortunately, there are also enough examples where the documentation is inaccurate.