You might have heard a lot of things about TypeScript. How much of it do you think is fake news and how much of it is true?
- TypeScript was built with the purpose of creating tools to increase developers' productivity (Anders Hejlsberg)
- You can catch errors before running your app
- You can refactor your code, it’s no longer read-only
- You can get autocompletion for nearly every package out there and for your own code
- Dial it all the way to zero or all dial it as high as you want (Anders Hejlsberg)
- Typescript doesn’t have to be explicit — you don’t have to add types to every single variable, TypeScript can infer types and have it automatically available for you
- TypeScript is very flexible with everything; you can use it and benefit a lot from it without making it a nuisance
TYPESCRIPT DOES NOT
- forcers you to use a pattern (like OO) — you can choose which patterns you want to use, closures, classes, or anything in between
Real World Examples?
Code from 2010, 3rd most voted answer in a 4.4m view thread, no one ever suggested any edits, comments, or anything about this issue. I only really saw this today as I was writing this post. This might look ok, but once we add TypeScript, we can immediately see an issue.
Remember you can be strict…or not …it’s up to you — don’t let strictness take away your freedom if that’s important to you, focus on all the features and the benefits TypeScript can bring to you.
How do I keep my code clean without being strict?
There are a couple of parameters in the config you can enable to check for unused variables so you can remove any unused code.
- “noUnusedLocals”: true,
- “noUnusedParameters”: true,
Clean Code is a MUST HAVE for every single project you work on. You should always do things like:
- Meaningful names — choose good names for your variables and functions so that you know what they are/do by just reading their names
- Avoid mental mapping — write code that you can understand by just reading as a book, if you have to think/do calculations in your head, then your code is not as clean as it could be
- Avoid disinformation — make sure you don’t put name on things that says that they do something they don’t
- Make meaningful distinction — choose specific names to avoid ambiguity
- Use searchable names — make sure you don’t have typos and use names that can be found a later time when you need to find things
- Be consistent — use same naming pattern for things that do the same thing
- Comments — don’t write comments unless there’s some extremely specific information that needs to be conveyed, or some intent, or some warnings about consequences. Otherwise, your code should be written in a way that you can read as if it was a book. Or if you’re writing some regexes
- don’t write shit code that causes a lot of bugs or makes it impossible to develop new features
- don’t overengineer
- don’t make things more complicated than they need to be right now (YAGNI & DTSTTCPW engineering & features) / don’t worry about the beauty and elegance of your code
- don’t focus on trends
- don’t design & develop features that don’t work
- don’t waste time discussing a bike shed