Category: javascript
What and Why TypeScript
Published on 20 Apr 2026
Explanation
TypeScript is a strongly typed superset of
JavaScript developed by Microsoft.
It adds static
typing, interfaces, and modern features
to JavaScript
and compiles into plain JavaScript that runs
in browsers or Node.js.
Code:
let message: string = "Hello TypeScript"; console.log(message);
Explanation
TypeScript helps developers catch errors
during development
instead of runtime by enforcing type
safety.
Code:
let age: number = 25; // age = "twenty five"; // Error: Type 'string' is not assignable to type 'number'
Explanation
Explicit types make code easier to
understand
and maintain, especially in large projects.
Code:
function greet(name: string): string {
return "Hello " + name;
}
Explanation
TypeScript provides interfaces,
classes, access modifiers, and
inheritance for better structured
applications.
Code:
interface User {
name: string;
age: number;
}
const user: User = {
name: "Praveen",
age: 25
};
Explanation
TypeScript improves developer
productivity with auto-completion,
refactoring,
and error detection in editors like VS
Code.
Code:
function add(a: number, b: number): number {
return a + b;
}
Explanation
Frameworks like Angular and enterprise-level
applications prefer
TypeScript for scalability and
maintainability.
Code:
// Example Angular-style TypeScript class
class AppComponent {
title: string = "My App";
}