Outstanding Features of TypeScript: Static Type Checking, Compiler, Module System

Static Type Checking Support

One of the strengths of TypeScript is its ability to perform static type checking. With this feature, we can define and apply data types to variables, function parameters, and return values.

For example:

let age: number = 25;
let name: string = "John";
let isActive: boolean = true;

In the above example, we declare variables age of type number, name of type string, and isActive of type boolean. TypeScript will check the validity of assignments and report errors if any discrepancies are found.

 

Compiler and Automation Support

TypeScript comes with a powerful compiler that transpiles TypeScript code into equivalent JavaScript code. Additionally, TypeScript provides automation tools for tasks such as error fixing, code formatting, and syntax checking, enhancing productivity and reducing effort during development.

For example:

// TypeScript code
const sum = (a: number, b: number): number => {
  return a + b;
};

// Transpiled JavaScript code
var sum = function (a, b) {
  return a + b;
};

 

Compile-Time Error Checking

TypeScript performs error checking at compile time, detecting logical errors, syntax mistakes, and type-related issues before running the application.

For example:

const calculateArea = (radius: number): number => {
  return Math.PI * radius * radius;
};

console.log(calculateArea("5")); // Lỗi: kiểu dữ liệu không phù hợp

In the above example, TypeScript will catch the error during compilation as we pass a string "5" to a parameter radius of type number.

 

Module System Support

TypeScript supports a robust module system, allowing for the division of source code into independent modules. This enhances code management, reusability, and scalability.

For example:

// Module A
export const greeting = "Hello";

// Module B
import { greeting } from "./moduleA";
console.log(greeting); // Kết quả: "Hello"

In the above example, we have two modules, moduleA and moduleB. moduleA exports a variable greeting, and moduleB imports the greeting variable from moduleA and uses it.

 

Extended Syntax and Features

TypeScript extends the syntax and features of JavaScript. For example, TypeScript supports the latest ECMAScript features like arrow functions, async/await, destructuring, and template literals. This allows developers to leverage modern features and write more readable and understandable code.

For example:

const name = "John";
const message = `Hello, ${name}! Welcome to TypeScript.`;
console.log(message); // Kết quả: "Hello, John! Welcome to TypeScript."

In the above example, we use template literals to create a string that includes the name variable.

 

In summary, TypeScript has outstanding features such as static type checking, compiler and automation support, compile-time error checking, module system support, and extended syntax and features. These features enhance reliability, performance, and code management during application development.