Disclaimer: If you believe that Typescript is a conspiracy by Microsoft to turn Javascript into C# and you believe in the religion of monkey-patching objects, then please do not proceed further. This article will burn your soul if you read it
Ever since I have started using Typescript, I cannot imagine writing large projects in plain JS anymore. My relationship to Typescript is best explained by this very nicely articulated piece by Tom Dale
While Typescript can make working with frontend frameworks like Angular and Vue a whole new revelation, using it on backend projects can have equally pleasing results. What I like about Typescript is the strictness can be turned a few notches up or down based on your requirements. And as you keep turning the strictness up by notches, you start seeing Typescripting starting to block off all those cracks and crevices in your code that were sitting there, waiting for you to slip into them at runtime, and cause hard-to-debug errors.
Since Typescript brings in the concept of compilation before running the code, the obvious error-prone code blocks will be straightaway flagged by Typescript, and you'll have to fix them before you can even get to the running stage
Here is an example of a run of the mill Sequelize code -
https://gist.github.com/8186d080e7d0a23854112aefbca6021d
Usual points of failure of this code are at:
- Line A: What if I miss any keys while defining, or jumble up the properties like
type
anddefaultValue
- Line B: What if I miss any keys, or mis-type the key name
- Line C: What if I do not use correct key names when fetching data from
user
Most IDEs like Webstorm or VS Code will only give rough suggestions, and not very indicative autocompletes either.
When dealing with 15-20 models in your project (usual mid-size node/express projects get there pretty fast), you'd want a little more strictness around your models' keys and better autocomplete would always be welcome.
To start using Typescript, you'll need typescript
module, and to make better use of types from sequelize you should also get the @types/sequelize
module
https://gist.github.com/1bd326bafe8e6e48122d3c765ee08c6d
You can check out what the entire project structure looks like here, but here is the lowdown -
Here is what my /db/models/Client.ts
file looks like -
https://gist.github.com/a852aae1b530ae4e0842c837afabd139
And then once many such models are defined, my /db/index.ts
will look like this -
https://gist.github.com/c818d85d34badeb46a4903278f4d1be0
As long as you're using a good IDE (or even Vim with the appropriate plugins), you do not need to write those cumbersome boilerplates all over again. We can create Apache Velocity templates for the define statement and the model file.
Here is what my Model file template looks like -
NOTE: ${NAME}
is file name
https://gist.github.com/5f2d8dd0a5f72e2b8bfaa1bd84a53dcb
So as soon as I generate a file of name MyModel.ts all the scaffolding is already done for me.
Similarly I have created a live template to generate the db.define
statements -
https://gist.github.com/b8b52aa8d0cf2f81906b85fbc2d9ce21
Whenever I want to create objects, I can see the keys and their types in autocomplete
Unless the object I save is exactly right, I get a compile error
And I get similar fluent autocomplete in the query result item as well
And finally I get the same luxury on where
queries too
A little bit of investment into boilerplate; whether you type manually everytime - which IMHO is a bit cumbersome or you take advantage of your IDE/Editor and create templates; is required when using Typescript, but the advantages we get in terms of code completion and more importantly, compile-time error checking, even before we go into run-time mode, is well worth the time invested.
Could not have written without StackEdit.
Obligatory shameless self-plug :
I am one of the co-founders of Coding Blocks - A Software Programming bootcamp, based out of New Delhi, India. Among other things, we teach Full Stack Web Development using NodeJS, via both classroom programmes, as well as online classes. You can follow our Medium to find more articles on Android and Web development.