What is TypeScript?

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It is a programming language developed and maintained by Microsoft.

TypeScript adds type annotations to JavaScript, which allows developers to catch type-related issues before runtime and to improve the overall quality of their code. TypeScript also introduces features that are not present in JavaScript, such as interfaces, classes, and modules, which make it easier to write and maintain large-scale applications.

TypeScript is particularly useful for developing applications that use JavaScript libraries or frameworks such as Angular, React, or Vue.js, as it helps to reduce the risk of runtime errors and to improve code maintainability.

TypeScript code is written in .ts files and is compiled to .js files using the TypeScript compiler (tsc). To use TypeScript, you need to install the TypeScript compiler and a code editor that supports TypeScript.

Here is an example that illustrates some of the basic features of TypeScript:

// Declare a variable with a type annotation
let message: string = 'Hello, world!';

// Declare a function with a type annotation for the argument and the return value
function greet(name: string): string {
  return `Hello, ${name}!`;
}

// Call the function and print the result
console.log(greet('John'));

// Declare an interface for an object
interface Point {
  x: number;
  y: number;
}

// Declare a function that takes an object that implements the Point interface
function distance(p1: Point, p2: Point): number {
  let dx = p1.x - p2.x;
  let dy = p1.y - p2.y;
  return Math.sqrt(dx * dx + dy * dy);
}

// Call the function with two objects that implement the Point interface
let p1: Point = { x: 0, y: 0 };
let p2: Point = { x: 3, y: 4 };
console.log(distance(p1, p2));

// Declare a class with a constructor and a method
class Person {
  constructor(public name: string) {}
  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }
}

// Create an instance of the class and call the method
let person = new Person('Jane');
person.greet();

In this example, we declare a variable message with a type annotation of string, which means that the variable can only hold string values. We also declare a function greet() that takes a string argument and returns a string value. The type annotations for the argument and the return value ensure that the function is called with the correct type of value and that the correct type of value is returned.

We also declare an interface called Point that defines the shape of an object with two properties: x and y, both of type number. We use this interface to declare a function distance() that takes two Point objects and returns a number.

Finally, we declare a class Person with a constructor and a method greet().

The Person class has a single property called name, which is declared in the constructor using the public keyword. This means that the name property will be accessible from outside the class using the dot notation (e.g., person.name).

The Person class also has a method called greet(), which logs a message to the console introducing the person. The method refers to the name property using the this keyword, which refers to the current instance of the class.

To create an instance of the Person class, you can use the new operator and call the constructor with the desired name as an argument. For example:

let person = new Person('Mustafa');

This will create a new Person object with the name “Jane”. You can then call the greet() method on the object to log the introduction message to the console:

person.greet();  // Output: "Hello, my name is Mustafa"

This example illustrates how TypeScript helps to improve the quality of your code by adding type annotations and other features that are not present in plain JavaScript. By using TypeScript, you can catch type-related issues before runtime and write code that is easier to understand and maintain.

I hope this helps to give you a better understanding of how TypeScript works! Let me know if you have any questions or need further assistance.

Published by

mustafabugra

Systems Engineer

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.