Supercharge your development with unmatched features:
Access a full terminal environment, run Linux commands, and manage your project’s dependencies directly within the IDE.
Browse and interact with websites directly within the IDE. Supports real-time interaction with web content without leaving the workspace.
Manage your project files and directories effortlessly within the IDE. Create, edit, rename, move, and delete files—all in one place.
Experience seamless code editing with real-time syntax highlighting, tab support, and intelligent code suggestions for a smoother development workflow.
TypeScript is a superset of JavaScript that adds static types to the language. It helps catch errors early through a type system and enhances code quality with features like interfaces and classes.
In TypeScript, variables can be declared with specific types, which provides type safety during development.
let name: string = "John"; // String type
let age: number = 30; // Number type
let isActive: boolean = true; // Boolean type
TypeScript supports various data types like string
, number
, boolean
, any
, and more.
Functions in TypeScript can be typed, which ensures that parameters and return values match the expected types.
function greet(name: string): string {
return "Hello, " + name;
}
console.log(greet("Alice")); // Output: Hello, Alice
Function parameters and return types can be explicitly defined, ensuring better type safety.
Interfaces in TypeScript define the structure of an object. They can describe the shape of objects, ensuring type consistency in the application.
interface Person {
name: string;
age: number;
}
const user: Person = { name: "John", age: 25 }; // Correct type
// const invalidUser: Person = { name: "Jane" }; // Error: Missing age
Interfaces help ensure that objects have the correct properties and types.
TypeScript supports object-oriented programming through classes, which allow for the creation of objects and encapsulation of properties and methods.
class Car {
brand: string;
model: string;
constructor(brand: string, model: string) {
this.brand = brand;
this.model = model;
}
drive(): void {
console.log(`Driving a ${this.brand} ${this.model}`);
}
}
const myCar = new Car("Toyota", "Corolla");
myCar.drive(); // Output: Driving a Toyota Corolla
Classes allow for the creation of instances and defining methods that operate on the data within the class.
Generics allow the creation of reusable components that work with any data type, providing flexibility while maintaining type safety.
function identity(arg: T): T {
return arg;
}
console.log(identity(5)); // Output: 5
console.log(identity("Hello")); // Output: Hello
Generics make it possible to create functions and classes that are independent of specific data types.
Modules in TypeScript allow for organizing code into smaller, reusable parts. Modules are imported and exported to share functionality between different files.
// In file math.ts
export function add(a: number, b: number): number {
return a + b;
}
// In file app.ts
import { add } from './math';
console.log(add(3, 4)); // Output: 7
Modules help organize code and prevent namespace conflicts by encapsulating code within files.
TypeScript can automatically infer the type of a variable based on its value, reducing the need to explicitly define types in some cases.
let greeting = "Hello"; // TypeScript infers the type as string
greeting = 10; // Error: Type 'number' is not assignable to type 'string'
Type inference can be helpful, but explicit typing is recommended for complex scenarios to ensure type safety.
Type aliases allow you to define custom types, making it easier to work with complex types and improving code readability.
type Point = { x: number, y: number };
const point: Point = { x: 10, y: 20 }; // Custom type Point
Type aliases simplify complex type definitions and make the code more expressive.
Decorators in TypeScript are a special kind of declaration that can be attached to classes, methods, or properties to modify their behavior at runtime.
function log(target: any, key: string) {
console.log(`${key} method has been called`);
}
class MyClass {
@log
greet() {
console.log("Hello!");
}
}
const obj = new MyClass();
obj.greet(); // Output: greet method has been called
// Hello!
Decorators are useful for cross-cutting concerns like logging, validation, and others.