Why is it better to compile TS in JS in advance
node-ts), or compile themselves in advance (for example, through`
TypeScript should be compiled in advance, through specialized compilers (for example,
tsc) and run already JS code.
A couple of topical reasons:
You never know where you will deploy your code.
Today it is a server you control where you can use
node-ts, tomorrow it is AWS Lambda or Google App Engine, where you need to deploy JS code.
When switching from
node-tspre-compilation, unforeseen errors and fuss may occur with changes in the configuration of CI / CD and the like, therefore it is more reliable to start immediately with pre-compilation.
Catch if you can!
Disgusting, unobvious errors …
I came across one of them quite recently: I wrote a TypeScript application for Node.js, compiled it and installed it on Google App Engine.
Everything worked on the personal Mac, and an error appeared on the server that there is no file along the path
Error: Cannot find module 'some/folder/path/FileName.ts' at ...
One would think that the code simply does not compile, or I download the old version, or it crashes
NODE_PATHand the like, but everything turned out to be much worse:
Since I compiled in advance, I could see what kind of js code is sent to the server. And then it became clear that only this ONE file was not compiled with the original name:
FileName.ts– but with a small letter:
fileName.ts… It was
useful to find out what was the matter and it turned out:
(1) TS compiler names the compiled files not according to the name of their originals, but according with imports in code.
That is, if you wrote with a small letter
import * from "some/folder/path/fileName", and the original is called with a capital letter (
FileName.ts), then TS will compile it into a file with a small letter. Even if in the rest of the code there are everywhere imports with a capital letter:
import * from "some/folder/path/FileName"
(2) On the Node.js poppy, the application is not case sensitive, so everything worked quietly
(3) On the Google App Engine, the case is case sensitive …
I had the option to compile ts files on the fly and send them to Google App Engine and then I would spend too much time to find out a similar problem. But since I was going through pre-compilation, I could safely go into the JS files being sent and secure them.
If you use TS, then first compile it with the appropriate tools (tsc, webpack, etc.), and only then run / deploy to the desired platform.
So when TS has its own VM, then … we’ll compile for a couple of years until it appears in different ecosystems such as Google, Amazon, .etc, and now we’ll live by the way.
What do you think about this: are there any advantages to using it
node-ts? Have you encountered non-obvious problems without prior compilation?
Good luck to everyone, do not let debugging kill the happiness of coding!
Also, in order for JS to have no problems with the name at all, it is better to stick to the “kebab case”, as advised by Uncle Google.