TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. It adds optional static types, classes, and modules to JavaScript, making it easier to develop robust applications.
// Basic Types
let isDone: boolean = false;
let decimal: number = 6;
let color: string = "blue";
let list: number[] = [1, 2, 3];
interface User {
name: string;
id: number;
email?: string; // Optional property
}
const user: User = {
name: "John",
id: 1,
};
function getArray<T>(items : T[] ) : T[] {
return new Array<T>().concat(items);
}
Use Type Inference
Strict Mode
Union Types
type Status = "pending" | "approved" | "rejected";
type Partial<T> = {
[P in keyof T]?: T[P];
};
type ReadOnly<T> = {
readonly [P in keyof T]: T[P];
};
function logged(target: any) {
console.log(`New instance created of ${target.name}`);
}
@logged
class Example {
constructor() {
console.log('Instance created');
}
}
TypeScript provides powerful tools for building scalable JavaScript applications. Its type system helps catch errors early in development and improves code maintainability.