JavaScript is dynamically typed—variables can change from numbers to strings at runtime, causing bugs. TypeScript adds an optional type system on top of JavaScript. The TypeScript compiler (tsc) validates your code during development and strips away the types, emitting clean, standard JavaScript that runs everywhere.
Entity state & role in architecture: TypeScript Code (.ts)
// Explicit type annotations in TypeScript
let studentName: string = "Ada Lovelace";
let age: number = 24;
let isEnrolled: boolean = true;
// Type inference: TypeScript knows 'score' is a number!
let score = 98.5;
function calculateGrade(points: number): string {
if (points >= 90) return "Grade: A (Distinction)";
return "Grade: Pass";
}
console.log(`Student: ${studentName}`);
console.log(calculateGrade(score));let studentName: stringType annotation explicitly restricting studentName to text strings.
function calculateGrade(points: number): stringGuarantees parameter must be a number and return value must be a string.
let score = 98.5;Type inference automatically assigns the number type without manual annotation.