TypeScript is a superset of JavaScript that adds static typing and additional features like interfaces, enums, and generics. It helps developers catch errors at compile time rather than runtime, making code more robust, scalable, and maintainable.
TypeScript code is compiled (or transpiled) to plain JavaScript, which runs in any browser or JavaScript environment.
TypeScript offers several built-in types:
any
, but safernumber[]
, string[]
)[string, number]
)string | number
)'success' | 'error'
)Example:
let id: number = 5;
Explanation:
No — TypeScript enforces static typing.
Once a variable is assigned a type, either implicitly or explicitly, it cannot be changed to another type.
Example:
let name = "Vivek"; // TypeScript infers this as type 'string'
name = "Vernekar"; // ✅ Allowed
name = 123; // ❌ Error: Type 'number' is not assignable to type 'string'