TypeScript Features
Explore the important features of TypeScript, including static type checking, type inference, interfaces, classes, generics, and modern JavaScript support.
What Are TypeScript Features?
TypeScript provides additional programming features on top of JavaScript. These features help developers identify many errors during development and organize code for larger applications.
TypeScript works with the JavaScript ecosystem and is commonly compiled into JavaScript before execution.
Important Features of TypeScript
| Feature | Description | Example |
|---|---|---|
| Static Type Checking | Reports many type-related errors during development. | let age: number = 25; |
| Type Inference | Infers a variable's type from its initial value. | let course = "Full Stack"; |
| Interfaces | Describe the expected structure of objects. | interface Student { } |
| Classes | Support object-oriented programming. | class Student { } |
| Generics | Help create reusable, type-aware components. | function identity<T>(value: T): T |
| Modern JavaScript Support | Supports modern JavaScript syntax and features. | const greet = () => {}; |
1. Static Type Checking
Static type checking allows TypeScript to report incompatible assignments during development or compilation. Developers can declare types such as string, number, and boolean.
Example
let studentName: string = "Rahul";
let marks: number = 85;
console.log(studentName);
console.log(marks);
Output
Rahul
85
Type Error Example
let marks: number = 85;
// TypeScript reports a type error.
marks = "Eighty Five";
2. Type Inference
Type inference means TypeScript can determine a variable's type from its initial value. Explicit type annotations are not required for every variable.
Example
let courseName = "Full Stack Development";
let duration = 6;
let isOnline = true;
console.log(courseName);
console.log(duration);
console.log(isOnline);
Output
Full Stack Development
6
true
3. Interfaces
An interface describes the expected structure of an object. It is useful when multiple parts of an application work with the same data structure.
Example: CIIT Student
interface Student {
id: number;
name: string;
course: string;
}
const student: Student = {
id: 101,
name: "Priya",
course: "Dot Net Full Stack"
};
console.log(student.name);
console.log(student.course);
Output
Priya
Dot Net Full Stack
4. Classes and Objects
TypeScript supports classes, constructors, methods, inheritance, and access modifiers. These features help organize applications using object-oriented programming.
Example
class Course {
constructor(
public courseName: string,
public duration: number
) {}
displayDetails(): void {
console.log(this.courseName);
console.log(this.duration + " months");
}
}
const course = new Course("TypeScript", 3);
course.displayDetails();
Output
TypeScript
3 months
The Course class defines course properties and a method that displays course details.
5. Generics
Generics allow developers to build reusable functions, classes, and interfaces while preserving type information.
Example
function displayValue<T>(value: T): T {
return value;
}
console.log(displayValue<string>("CIIT"));
console.log(displayValue<number>(100));
Output
CIIT
100
6. Modern JavaScript Support
TypeScript supports modern JavaScript features, including arrow functions, template strings, destructuring, modules, and async/await.
Example: Arrow Function
const greetStudent = (name: string): string => {
return `Welcome to CIIT, ${name}!`;
};
console.log(greetStudent("Rahul"));
Output
Welcome to CIIT, Rahul!
7. TypeScript Compilation
TypeScript can check source code and generate JavaScript. A common workflow is to create an app.ts file and compile it using the TypeScript compiler.
Command
tsc app.ts
Result
If compilation succeeds, the compiler generally
generates app.js. That JavaScript file
can run in a compatible browser or JavaScript runtime.
Practical Example: CIIT Student Details
This example combines an interface, type annotations, and a function in a simple student management scenario.
Example
interface StudentDetails {
id: number;
name: string;
course: string;
}
function displayStudent(student: StudentDetails): void {
console.log("Student ID: " + student.id);
console.log("Student Name: " + student.name);
console.log("Course: " + student.course);
}
const student: StudentDetails = {
id: 101,
name: "Amit",
course: "Full Stack Development"
};
displayStudent(student);
Output
Student ID: 101
Student Name: Amit
Course: Full Stack Development
Important Points
- Static type checking helps identify incompatible types.
- Type inference reduces unnecessary annotations.
- Interfaces describe expected object structures.
- Classes support object-oriented programming.
- Generics help create reusable, type-aware code.
- TypeScript supports modern JavaScript syntax.
- TypeScript is commonly compiled into JavaScript.
Summary
TypeScript provides static type checking, type inference, interfaces, classes, generics, and modern JavaScript support. These features help developers build understandable and maintainable applications. In the next topic, we will explore the advantages of using TypeScript in real-world development.