Let’s Learn : Typescript

Rhea RB
2 min readJul 2, 2023

--

What is Typescript ?
Typescript is a superset of Javascript that adds static typing and other features to the language.

With Typescript you can catch errors at compile time instead of runtime, making it easier to write and maintain large scale applications.

Example.
Here, the name parameter of the great function is declared as a string using a type annotation. This means that if we try to call greet with a parameter that is not a string, Typescript will catch the error and give us a compile time error.

function hello(name: string) {
console.log("Hello", name)
}
hello("Rhea")

Type Annotations
Type annotations in typescript allow you to specify the type of variable, parameter or function return value.

This can help catch errors at compile time rather than at runtime and also provides more clarity to the code.

Type helps ensure that we’re using the correct types and catch any potential errors early in development process.

let myArray: number[] = [1,2,3] //annotations for array 
let a : string = 'Hello'; //annotations for string

function add(a:number, b: number) : number { //annotations for a function.
return a+b;
}

Interfaces
Interfaces in Typescript define the shape of an object. They allow us to specify the required properties and their types as well as optional properties functions and more.

interface Person {
firstName: string,
lastName: string
}

function greet(person: Person) {
console.log(`hello ${person.firstName} ${person.lastName}`);
}

Typescript Benefits.

Type safety:
It provides static type checking which helps catch errors before runtime. This can lead to fewer bugs, better maintainability and more code confidence.

Scalability:
It is designed to scale a large codebase. Its type system makes it easier to refactor code and maintain consistency across large projects.

Better code quality:
By catching errors earlier, Typescript can help improve code quality. It can also encourage better coding practices such as writing more modular, reusable code.

Javascript interpolability:
It is a superset of Javascript, so it can easily integrate with existing Javascript code. This makes it a good choice for gradually adopting it in existing projects.

Thank you for Reading !

If you liked my blog , please do follow me on Rhea RB

Happy Coding !!

--

--

Rhea RB

Hello ! I am a senior software engineer, passionate about tech and product.