Sitemap
Manoranjan Jena

Manoranjan Jena

TypeScript

Getting Started

3 min readMay 2, 2023

--

Press enter or click to view image in full size

TypeScript is a powerful programming language designed as a superset of JavaScript. Developed by Microsoft, TypeScript was first released in 2012 and has since become an increasingly popular choice among developers for building complex and scalable web applications. In this blog, we will explore the basics of TypeScript and how it can improve the development process.

What is TypeScript?

TypeScript is a statically-typed programming language that adds optional types to JavaScript. This means that we can specify the types of variables, function parameters, and function return values in our code. TypeScript then uses these types to catch errors at compile-time, before the code is actually run.

TypeScript also introduces other features not available in JavaScript, such as interfaces, classes, and decorators. These features make it easier to write large, complex applications by providing a way to organize code and define reusable components.

Installing TypeScript

Before we dive into writing TypeScript code, we need to install the TypeScript compiler. We can do this using npm, the package manager for Node.js. Open a terminal or command prompt and run the following command:

npm install -g typescript

This will install the TypeScript compiler globally on your system.

Writing TypeScript

Let’s start by creating a simple TypeScript file. Create a new file called hello.ts and add the following code:

function sayHello(name: string) {
console.log(`Hello, ${name}!`);
}

sayHello('TypeScript');

In this example, we have defined a function called sayHello that takes a parameter called name of type string. We then call this function with the argument 'TypeScript'. Notice that we did not have to specify the return type of the function, as TypeScript can infer it from the fact that we are calling console.log, which returns void.

Get Manoranjan Jena’s stories in your inbox

Join Medium for free to get updates from this writer.

To compile this file, we can run the following command in the terminal:

tsc hello.ts

This will generate a JavaScript file called hello.js that contains the same code, but with the type annotations removed:

function sayHello(name) {
console.log(`Hello, ${name}!`);
}

sayHello('TypeScript');

Using Interfaces

One of the powerful features of TypeScript is the ability to define interfaces. An interface is a way to define a contract between different parts of your code, specifying what properties and methods an object should have.

Let’s create an interface for a person:

interface Person {
firstName: string;
lastName: string;
age: number;
}

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

const john: Person = {
firstName: 'John',
lastName: 'Doe',
age: 30,
};

printPerson(john);

In this example, we define an interface called Person that has three properties: firstName, lastName, and age. We then define a function called printPerson that takes an argument of type Person and logs the person's name and age to the console.

We then create an object called john that conforms to the Person interface, and pass it to the printPerson function.

Using Classes

Another feature of TypeScript is the ability to define classes. Classes provide a way to define objects with properties and methods, and are a cornerstone of object-oriented programming.

Let’s create a simple class for a car:

class Car {
private make: string;
private model: string;
private year: number;

constructor(make: string, model: string, year: number) {
this.make = make;
this.model = modelthis.year = year;
}

public getInfo(): string {
return ${this.make} ${this.model} (${this.year});
}
}

const myCar = new Car('Honda', 'Civic', 2020);
console.log(myCar.getInfo());

In this example, we define a class called Car that has three private properties: make, model, and year. We then define a constructor that takes these properties as arguments and sets them on the object. We also define a public method called Info that returns a string with the car’s make, model, and year. We then create an instance of the Car class called myCar, passing in the make, model, and year as arguments to the constructor. We then call the getInfo method on myCar and log the result to the console.

Conclusion

In this blog, we have covered the basics of TypeScript, including how to install the compiler, how to write TypeScript code, and how to use interfaces and classes. TypeScript is a powerful language that can greatly improve the development process by catching errors at compile-time and providing additional features not available in JavaScript. If you’re interested in learning more about TypeScript, check out the official documentation and start experimenting with the language. Happy coding!

--

--