Apr 26, 2025
Installation
npm install typescript --save-devInitialize Configuration
npx tsc --initTS Config File
String Variable Example:
let name: string = 'John Doe';Number Variable Example:
let discount: number;Boolean Variable Example:
let isBetaUser: boolean;String Array Example:
let topSellers: string[]; to restrict array values.Union Type for Arrays:
let topSellers: (string | number)[];function calculateLoan(amount: number, rate: number): number {}Interfaces:
interface Employee {
id: number;
name: string;
position?: string; // Optional property
}
Type Aliases:
type Username = string;).type Role = 'admin' | 'user';).enum Subscription { Free, Basic, Premium }
function identity<T>(arg: T): T {
return arg;
}