TypeScript can help you get shit done faster
--
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?
TRUE facts:
- TypeScript is a superset of JavaScript
- TypeScript was built with the purpose of creating tools to increase developers' productivity (Anders Hejlsberg)
- All JavaScript code is TypeScript code — you don’t need to learn a new language
- JavaScript with the future features available today (TypeScript implements proposals on ECMAScript Stage 3)
- 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
- have classes — JavaScript has classes, all that TypeScript does is to translate JavaScript code into today’s JavaScript code
- forcers you to use a pattern (like OO) — you can choose which patterns you want to use, closures, classes, or anything in between
- compare to CoffeeScript or similar things — comparing TypeScript to these things is like comparing apples to carburetors (Scott Hanselmann); TypeScript is not a syntax sugar for JavaScript, it is JavaScript, meaning the syntax is the same
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.
If you want to migrate from JavaScript to TypeScript, you might find some cases where strict might be annoying.
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,
But also…
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
Find balance
- 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