In the 2022 Stack Overflow survey, Typescript placed top 5 in the list of languages most loved by engineers and 2nd in the list for what engineers want to be using.

TypeScript is a strongly-typed superset of JavaScript that provides type safety, which means that it helps you to catch errors at compile time, rather than runtime. This makes it easier to write and maintain by improving code readability and maintainability. It does this by adding type annotations to your code which means you make it easier for other engineers to understand what types of values, functions and variables are expecting and returning.

What’s TypeScript?

TypeScript was originally developed by Microsoft back in 2012. Syntactically it’s very similar to Javascript. In fact, all JavaScript is valid as TypeScript code. The TypeScript compiler simply compiles TypeScript code into JavaScript which is interpreted by the browser as plain ol’ vanilla JavaScript. Essentially TypeScript is just JavaScript, with static typing.

What’s Static Typing?

A language is statically typed if the type of a variable is known at compile time, before the code is executed.

JavaScript is dynamically typed the interpreter assigns variables a type at runtime based on the variable's value after the code is executed.

Let’s look at an example to understand what this really means.


_10
let value = 5;
_10
value = "hello";

Here, the type of value changes from a number to a string. In most circumstances this is probably not a wise idea and is likely to cause an error in our code at a later point, for example:


_10
let value = "5";
_10
console.log(value + 2);

We would probably want the output to be “7” of this operation, right? In the wacky world JavaScript, this won’t be the case. Instead the output is “52”.

We are adding is a number to a string so the output will be converted to a string. JavaScript politely concatenates the two strings together, and the result is “52”.

In TypeScript, this is type of operation is forbidden. Our IDE will show us an error message while we write the code, or it will throw an error when we go to compile our TypeScript code to JavaScript.


_10
let value:number = 5;
_10
value = "hello"; // error: Type '"hello"' is not assignable to type 'number'

If you use JavaScript, you’re not likely to know what the type for some object is. It’s possible you won’t realise what methods a certain object has, or even what fields it has. It will run just fine, until you need to use the result of that property, causing a semantic error.


_15
class MyClass {
_15
_15
private value: number;
_15
_15
constructor(value: number) {
_15
this.value = value;
_15
}
_15
_15
getNumber(): number {
_15
return this.value;
_15
}
_15
}
_15
_15
let object = new MyClass(5);
_15
console.log(object.number); //"undefined"

With TypeScript, the compiler tells you what the problem is, so you are immediately aware that the code you’re writing doesn’t work. A few seconds of specifying types will save you from an hour of debugging.


_15
class MyClass {
_15
_15
private value: number;
_15
_15
constructor(value: number) {
_15
this.value = value;
_15
}
_15
_15
getNumber(): number {
_15
return this.value;
_15
}
_15
}
_15
_15
let object = new MyClass(5);
_15
console.log(object.number); // compiler error: Property 'number' does not exist on type 'ExampleCla

Enums

Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums. TypeScript uses syntactical sugar to make it work much better.


_10
_10
enum Colour {
_10
Red,
_10
Green,
_10
Blue,
_10
}
_10
_10
let c: Colour = Colour.Green;
_10
c = "Orange" // "Orange isn't in the Colours enum, so TypeScript will be upset.

You can do something similar in Javascript, but it doesn’t really work.


_10
_10
_10
var Color = {
_10
Red: 0,
_10
Green: 1,
_10
Blue: 2,
_10
}
_10
_10
var c = Color.Green;
_10
c = 6839; // You can do this, but it will cause problems in the logic of our code "

Literal Types

TypeScript supports string , booleanand number literal types. A literal type defines a type that accepts specified string literal. Use the string literal types with union types and type aliases to define types that accept a finite set of string literals.


_10
type Animal = "Dog" | "Monkey"
_10
_10
const animal: Animal = "Dog" // yay!
_10
const anotherAnimal: Animal = "Swan" // compiler error, an animal can only be a "Dog" oir "Monkey"

Stay ahead of the pack 🐶

Join the newsletter to get the latest articles and expert advice directly to your inbox.
Totally free, no spam and you can unsubscribe anytime you want!

  • Expert Tips
  • No Spam
  • Latest Updates

I'll never share any of your information with a third party. That's a promise.

Interfaces

You can enforce the structure of certain elements using interfaces. An interface defines the syntax that any entity must adhere to, you can think of it as a kind of contract.


_16
type Specie = "Dog" | "Monkey"
_16
interface Animal {
_16
name: string;
_16
says: string,
_16
specie: Specie,
_16
talk: () => void
_16
_16
}
_16
_16
const animal: Animal = {
_16
type: "Dog", // could also be Specie.Dog
_16
name: "Bruce",
_16
says: "woof"
_16
}
_16
_16
console.log(`${animal.name} the ${animal.specie} says "${animal.says}"!`); // Bruce the Dog says "woof"!

Typescript is a really great contribution to the JavaScript ecosystem. It offers many features the biggest one we detail is static typing which allows you and your team to work quicker and prevent regressions by being warning you about potential bug causing code via IDE auto-completion, type checking, and code navigation. It’s backwards compatible meaning it’s easy for to adopt without having to rewrite your entire codebase and supports object-oriented programming features such as classes, interfaces, and inheritance, making it easier for your team to write clean, maintainable code.

At the time of writing, TypeScript isn’t supported by Node.js, neither is it supported by any Internet browsers, so to use it, you’ll need to compile it to JavaScript which your can do easily with tsc. Newer runtimes such as Deno and Bun do support it out of the box, meaning TypeScript isn’t going anywhere, so try it out and start making your life a little easier!

Thanks alot for your feedback!

The insights you share really help me with improving the quality of the content here.

If there's anything you would like to add, please send a message to:

[email protected]

Was this article this helpful?

D is for danny

About the author

Danny Engineering

A software engineer with a strong belief in human-centric design and driven by a deep empathy for users. Combining the latest technology with human values to build a better, more connected world.

Related Articles

Gobacktothetop

Made with 🐾 in 🏴󠁧󠁢󠁥󠁮󠁧󠁿

©2024 All rights reserved.