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
  • JavaScript with the future features available today (TypeScript implements proposals on ECMAScript Stage 3)
Optional Chaning
  • You can catch errors before running your app
Catch errors in compile-time rather than runtime
  • You can refactor your code, it’s no longer read-only
CTRL/CMD + D
F2 (rename with the help of TypeScript)
  • You can get autocompletion for nearly every package out there and for your own code
React + Material UI
Lodash
Your own stuff
  • Dial it all the way to zero or all dial it as high as you want (Anders Hejlsberg)
not strict
strict
  • 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

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.

TypeScript is saying that the “if” condition is never ever going to be true, because the types `array` and `string` have no overlap.

If you want to migrate from JavaScript to TypeScript, you might find some cases where strict might be annoying.

strict can be a pain in real world code; in this case if text is null the function will return `null as string` which is not assignable to `boolean`
strict off; works exactly as intended

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,

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

Find balance

  • don’t write shit code that causes a lot of bugs or makes it impossible to develop new features

See