Skip to main content

Typescript, explained.

Published 2021/05/12 14:00

What is TypeScript? You've heard about it as the perfect companion to Javascript. But what is it and how does it work?

Let's explain!

Take a look at this Javascript code:

var number1 = 5
var number2 = 10

var sum = number1 + number2

console.log(sum)

What is the output of the code?

If your answer is 15 then you're right! You have two numbers, number1 and number2. If you add them together using the + sign, it will give you the total because they're both numbers!

Now take a look at this:

var number1 = "5"
var number2 = "10"

var sum = number1 + number2

console.log(sum)

This time number1 and number2 are strings. So what will this code give as output?

If your answer is "510" as a string, then you're right again!

But that's not how you expect it to work.

I want number1, number2, and sum to all be numbers so that sum gives me the actual sum every time.

Wouldn't it be nice if we could put some rules in so that we're absolutely CERTAIN that number1, number2 and sum are ALWAYS numbers?

Let's look at another example:

Suppose, this time, we want to divide two numbers together:

var number1 = 10
var number2 = 2

var result = number1 / number2

console.log(result)

The result of the above is (as you'd expect), 5. But what if instead of a number, number2 is a string, or an array?

var number1 = 10
var number2 = []

var result = number1 / number2

console.log(result)

What would you expect it to happen here?

If your answer is "it would give me an error" then you'd be wrong. The result would be "Infinity". But that is non-sense!

There should be some rule in place that says "you can only divide numbers with numbers". Wouldn't that be nice?

Enter TypeScript!

TypeScript is a language that is basically extended Javascript.

In essence, TypeScript allows you to write Javascript so that when you declare variables or functions, you can also specify what type each of them are (string, number, array etc).

For example:

"I want to create a variable called total which is a number":

var total:number = 5

"I want to create a variable name which is a string":

var name:string = "Savvas"

"I want to create a function getTotal which should always get a number array as input and always return a number":

function getTotal(values:number[]):number{
    return values.reduce((accumulator, currentValue) => {
        accumulator + currentValue
    })
}

So what would happen if I write a string in the total variable in my TypeScript (.ts) file, since it expects a number?

var total:number = "ABC"

This:

An error: Type 'string' is not assignable to type 'number'

This is what's so amazing about TypeScript. It tells you that what you're assigning is wrong before you even hit save! That means this error never makes it to your users, and it makes fixing errors a lot LOT easier!

So how do I get started?

You can install TypeScript using via NPM:

npm install -g typescript

You can then turn a TypeScript (.ts) file into a Javascript (.js) file by running this command:

npx tsc myfile.ts

You can then attach your JS file to your HTML as normal!

This thread only touches the absolute basics of TypeScript but you can learn a lot more on the TypeScript official documentation which is quite comprehensive!

Enjoy!

Dev, Explained (43 part series)

  1. Javascript Scopes, explained.
  2. Javascript Promises, explained.
  3. Accessibility, explained.
  4. React, explained
  5. Should I use forEach() or map()?
  6. Should I use Flexbox or CSS Grid?
  7. Docker, explained.
  8. Unit testing, explained
  9. Git, explained.
  10. Typescript, explained.
  11. async/await, explained.
  12. The DOM, explained.
  13. Regular expressions, explained
  14. GraphQL, explained.
  15. Vue, explained.
  16. Svelte, explained.
  17. API, explained.
  18. Javascript Hoisting, explained.
  19. Immediately Invoked Function Expressions (IIFE), explained.
  20. ARIA roles, explained.
  21. Test-driven Development, explained.
  22. ARIA live regions, explained.
  23. aria-label in accessibility, explained.
  24. Type coercion in Javascript, explained.
  25. Variables, explained.
  26. if statements, explained.
  27. Arrays, explained.
  28. Currying in Javascript, explained.
  29. Memoization, explained.
  30. For loops, explained.
  31. Javascript Prototypes, explained.
  32. React Hooks, explained.
  33. Graph databases, explained.
  34. MongoDB, explained.
  35. Serverless, explained.
  36. Javascript Callback functions, explained.
  37. HTML, explained.
  38. CSS, explained.
  39. Responsive design, explained.
  40. Javascript, explained.
  41. The CSS Box Model, explained.
  42. CSS Flexbox, explained.
  43. CSS Grid, explained.
2022 Savvas Stephanides
Buy me a coffee
Some icons from Freepik