***1. What is TypeScript?
TypeScript is an open-source programming language developed by Microsoft that is built on top of JavaScript.
It is a superset of JavaScript, which means:
- All JavaScript code is valid TypeScript code.
- TypeScript adds extra features like:
- Static typing
- Interfaces
- Classes
- Generics
- Advanced tooling support
TypeScript code is converted (compiled/transpiled) into JavaScript before running in the browser or Node.js environment.
Example
function greet(name: string): string {
return "Hello " + name;
}
console.log(greet("Jitendra"));
Compiled JavaScript
function greet(name) {
return "Hello " + name;
}
console.log(greet("Jitendra"));
Key Points for Interview
- TypeScript = JavaScript + Type Safety
- It helps detect errors during development.
- It improves code readability and maintainability.
- Widely used in Angular, React, Node.js, and enterprise applications.
***2. Why do we need TypeScript?
JavaScript is dynamically typed, which can cause runtime errors that are difficult to debug in large applications.
TypeScript is needed to:
- Improve code quality
- Detect errors early during development
- Make large-scale applications easier to maintain
- Improve developer productivity with better IDE support
Problems in JavaScript
function add(a, b) {
return a + b;
}
add(10, "20"); // Unexpected result: "1020"
Because JavaScript does not enforce data types, bugs may occur at runtime.
TypeScript Solution
function add(a: number, b: number): number {
return a + b;
}
add(10, 20); // Correct
If we pass a string, TypeScript shows an error during development.
Why Companies Prefer TypeScript
- Better maintainability
- Safer refactoring
- Easier teamwork in large projects
- Improved scalability
- Better IntelliSense and auto-completion
Key Interview Points
- Prevents runtime errors
- Supports large applications
- Provides static typing
- Improves development speed
***3. Why should we use TypeScript instead of JavaScript?
TypeScript provides several advanced features that JavaScript does not provide directly.
Developers use TypeScript because it makes applications:
- More reliable
- Easier to scale
- Easier to maintain
Major Reasons to Use TypeScript
| Feature | JavaScript | TypeScript |
|---|---|---|
| Static Typing | ❌ | ✅ |
| Compile-Time Error Checking | ❌ | ✅ |
| Interfaces | ❌ | ✅ |
| Generics | ❌ | ✅ |
| Better IDE Support | Limited | Excellent |
| Easier Refactoring | ❌ | ✅ |
Example
JavaScript
let age: number = 25; age = "twenty five"; //AllowedTypeScript
let age: number = 25;age = "twenty five";// Allowed
Benefits Over JavaScript
- Early bug detection
- Cleaner code structure
- Better code documentation
- Easier debugging
- Better collaboration in teams
Interview Tip
Interviewers often ask:
“If JavaScript already exists, why TypeScript?”
Best answer:
“TypeScript improves JavaScript development by adding static typing, better tooling, and scalability support for large applications.”
***4. What are the differences between TypeScript and JavaScript?
| Feature | JavaScript | TypeScript |
|---|---|---|
| Developed By | Netscape | Microsoft |
| Typing | Dynamic | Static |
| Compilation | No compilation required | Compiles to JavaScript |
| Error Detection | Runtime | Compile Time |
| OOP Support | Partial | Strong |
| Interfaces | ❌ | ✅ |
| Generics | ❌ | ✅ |
| Tooling Support | Moderate | Excellent |
| Learning Curve | Easy | Slightly Higher |
| File Extension | .js | .ts |
Example
JavaScript
let value = 10;
value = "Hello"; // Allowed
TypeScript
let value: number = 10;
value = "Hello"; // Error
Key Interview Point
TypeScript improves JavaScript by adding type safety and advanced development features.
**5. What are the advantages/benefits of using TypeScript?
1. Static Typing
Helps catch errors during development.
let price: number = 100;
2. Better IDE Support
Provides:
- Auto-completion
- IntelliSense
- Navigation
- Refactoring support
3. Early Error Detection
Errors are found at compile time instead of runtime.
4. Better Code Maintainability
Makes large projects easier to manage.
5. Supports Modern JavaScript Features
Supports ES6+ features and compiles to older JavaScript versions.
6. Improved Readability
Type definitions make code self-documenting.
7. Strong Object-Oriented Features
Supports:
- Classes
- Interfaces
- Access modifiers
- Inheritance
Key Interview Point
TypeScript improves productivity, maintainability, and application reliability.
*6. What are the disadvantages of using TypeScript?
Although TypeScript has many advantages, it also has some limitations.
Disadvantages
1. Compilation Required
TypeScript must be compiled into JavaScript before execution.
2. Learning Curve
Developers need to learn:
- Types
- Interfaces
- Generics
- Advanced concepts
3. Increased Development Time Initially
Writing type definitions may take extra time in the beginning.
4. More Configuration
Requires setup files like:
tsconfig.json
5. Not Executed Directly by Browsers
Browsers understand JavaScript, not TypeScript.
Interview Tip
Always mention:
“The advantages of TypeScript generally outweigh its disadvantages, especially for large-scale applications.”
**7. What are the features of TypeScript?
Major Features of TypeScript
1. Static Typing
let username: string = "Jitendra";
2. Object-Oriented Programming Support
Supports:
- Classes
- Interfaces
- Inheritance
- Encapsulation
3. Interfaces
interface User {
name: string;
age: number;
}
4. Generics
function identity<T>(value: T): T {
return value;
}
5. Type Inference
Automatically detects data types.
let city = "Patna";
6. Access Modifiers
Supports:
- public
- private
- protected
7. Compatibility with JavaScript
Existing JavaScript projects can gradually migrate to TypeScript.
8. Modern ES Features
Supports:
- Arrow functions
- Async/await
- Modules
- Destructuring
Key Interview Point
TypeScript combines JavaScript flexibility with strong typing and modern development features.
*8. What are the components of TypeScript?
TypeScript mainly consists of the following components:
| Component | Description |
|---|---|
| TypeScript Language | Includes syntax, types, and features |
| TypeScript Compiler (TSC) | Converts TypeScript into JavaScript |
| TypeScript Language Services | Provides IDE support like IntelliSense and error checking |
1. TypeScript Language
Provides:
- Types
- Interfaces
- Classes
- Modules
2. TypeScript Compiler (TSC)
Command:
tsc app.ts
Converts:
app.ts
into:
app.js
3. TypeScript Language Services
Improves developer experience with:
- Auto-completion
- Error detection
- Refactoring tools
Interview One-Liner
“The core components of TypeScript are the TypeScript language, compiler, and language services.”
*9. Who developed TypeScript and what is the latest stable version?
TypeScript was developed by Microsoft.
The main creator of TypeScript is Anders Hejlsberg, who is also known for creating C#.
Important Facts
- First released in: 2012
- Developed and maintained by: Microsoft
- File Extension:
.ts
Latest Stable Version
The latest stable version changes frequently.
You can check the official latest version here:
Interview Tip
Instead of guessing the exact version number in interviews, say:
“TypeScript is actively maintained by Microsoft, and the latest stable version can be checked on the official TypeScript website.”
**10. How do you install TypeScript?
TypeScript is installed using Node Package Manager (NPM).
Step 1: Install Node.js
Download and install Node.js from:
Step 2: Install TypeScript Globally
npm install -g typescript
Step 3: Verify Installation
tsc --version
This command displays the installed TypeScript version.
Step 4: Create TypeScript File
// app.ts
let message: string = "Hello TypeScript";
console.log(message);
Step 5: Compile TypeScript File
tsc app.ts
This generates:
app.js
Step 6: Run JavaScript File
node app.js
Interview Tip
Interviewers may ask:
“What command is used to install TypeScript globally?”
Answer:
npm install -g typescript
TypeScript files are compiled into JavaScript using the TypeScript Compiler (TSC).
The compiler converts .ts files into .js files because browsers understand JavaScript, not TypeScript.
Step-by-Step Compilation
Step 1: Create a TypeScript File
let message: string = "Hello TypeScript";
console.log(message);
Save it as:
app.ts
Step 2: Compile the File
Use the following command:
tsc app.ts
Step 3: Generated JavaScript File
After compilation:
app.js
is generated automatically.
Step 4: Run the JavaScript File
node app.js
Important Interview Points
-
tscstands for TypeScript Compiler. - TypeScript code cannot run directly in browsers.
- Compilation converts TypeScript into plain JavaScript.
One-Line Interview Answer
“We compile a TypeScript file using the
tsc filename.tscommand.”
12. Can TypeScript files compile automatically on changes?
Yes. TypeScript supports automatic compilation using Watch Mode.
Watch mode continuously monitors TypeScript files and recompiles them whenever changes are detected.
Command for Auto Compilation
tsc app.ts --watch
OR
tsc --watch
Short form:
tsc -w
How It Works
- TypeScript watches the file.
- When you save changes:
- It recompiles automatically.
- Updated JavaScript is generated instantly.
Benefits
- Faster development
- Saves manual compilation time
- Useful in large projects
Example Workflow
app.ts --> Automatically Compiled --> app.js
Important Interview Point
Watch mode is commonly used during development to improve productivity.
*13. What is tsconfig.json?
tsconfig.json is the configuration file used by TypeScript projects.
It defines:
- Compiler options
- Project settings
- Files to include/exclude
- Target JavaScript version
Why It Is Important
Instead of passing compiler settings manually every time, we define them once inside tsconfig.json.
Example
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"strict": true,
"outDir": "./dist"
}
}
Common Compiler Options
| Option | Purpose |
|---|---|
| target | JavaScript version output |
| module | Module system |
| strict | Enables strict type checking |
| outDir | Output folder |
| rootDir | Source folder |
| sourceMap | Generates source maps |
Create tsconfig.json
tsc --init
Key Interview Points
- Central configuration file for TypeScript projects
- Improves maintainability
- Required in most enterprise applications
One-Line Interview Answer
“tsconfig.json is the configuration file that controls TypeScript compiler behavior and project settings.”
14. What are source map (.map) files in TypeScript?*
Source map files (.map) help developers debug TypeScript code in the browser.
They map the generated JavaScript code back to the original TypeScript code.
Why Source Maps Are Needed
Browsers execute JavaScript, not TypeScript.
Without source maps:
- Debugging becomes difficult
- Error locations may not match original TypeScript code
Example
app.ts
Compiles into:
app.js
app.js.map
Enable Source Maps
Inside tsconfig.json:
{
"compilerOptions": {
"sourceMap": true
}
}
Benefits
- Easier debugging
- Better developer experience
- Shows original TypeScript in browser developer tools
Important Interview Point
Source maps connect JavaScript code with original TypeScript source code for debugging purposes.
*15. What are the built-in data types in TypeScript?
TypeScript provides several built-in data types to define the type of variables.
Main Built-in Data Types
| Data Type | Description |
|---|---|
| number | Numeric values |
| string | Text values |
| boolean | true/false |
| null | Empty value |
| undefined | Undefined variable |
| any | Any type of value |
| void | No return value |
| never | Value never occurs |
| object | Non-primitive values |
| array | Collection of values |
| tuple | Fixed-size array |
| enum | Named constants |
| unknown | Safer alternative to any |
Examples
let age: number = 25;
let name: string = "Jitendra";
let isActive: boolean = true;
Key Interview Point
TypeScript data types improve type safety and reduce runtime errors.
*16. Explain the data types available in TypeScript.
TypeScript data types are categorized into different groups.
1. Number
Used for numeric values.
let price: number = 100;
2. String
Used for text values.
let username: string = "Jitendra";
3. Boolean
Stores true or false.
let isLoggedIn: boolean = true;
4. Array
Stores multiple values.
let marks: number[] = [90, 80, 70];
5. Tuple
Fixed-size array with defined types.
let user: [string, number] = ["Jitendra", 25];
6. Enum
Represents named constants.
enum Role {
Admin,
User
}
7. Any
Can hold any value.
let value: any = "Hello";
8. Void
Used mainly for functions with no return value.
function print(): void {
console.log("Hello");
}
9. Never
Represents values that never occur.
function throwError(): never {
throw new Error("Error");
}
10. Unknown
Safer alternative to any.
let data: unknown;
Interview Tip
Interviewers commonly ask:
“Difference between any, unknown, void, and never.”
Prepare these properly.
17. What are user-defined data types in TypeScript?*
User-defined data types are custom types created by developers according to application requirements.
Types of User-Defined Data Types
| Type | Description |
|---|---|
| Interface | Defines object structure |
| Class | Blueprint for objects |
| Enum | Named constants |
| Type Alias | Custom type names |
1. Interface
interface Employee {
id: number;
name: string;
}
2. Class
class Person {
name: string = "Jitendra";
}
3. Enum
enum Direction {
Up,
Down
}
4. Type Alias
type ID = number | string;
Important Interview Point
User-defined data types help create reusable and scalable code structures.
*18. What is the any type in TypeScript?
The any type allows a variable to store values of any data type.
It disables type checking for that variable.
Example
let value: any = 10;
value = "Hello";
value = true;
Why It Is Used
Used when:
- Type is unknown
- Migrating JavaScript to TypeScript
- Handling dynamic data
Disadvantages
- Removes type safety
- Can introduce runtime errors
- Reduces TypeScript benefits
Interview Tip
Avoid excessive use of any.
Prefer:
- specific types
-
unknown - interfaces
One-Line Interview Answer
“The
anytype allows storing any type of value and disables TypeScript type checking.”
19. What is the void type in TypeScript?
The void type represents the absence of a value.
It is mainly used as the return type of functions that do not return anything.
Example
function greet(): void {
console.log("Welcome");
}
Important Points
- A
voidfunction does not return a value. - Commonly used in utility functions.
Example with Arrow Function
const printMessage = (): void => {
console.log("Hello");
};
Interview Tip
Difference between:
-
void→ function returns nothing -
never→ function never finishes
*20. What is the never type in TypeScript?
The never type represents values that never occur.
It is used when:
- A function never returns
- A function always throws an error
- Infinite loops exist
Example 1: Function Throwing Error
function throwError(message: string): never {
throw new Error(message);
}
Example 2: Infinite Loop
function infiniteLoop(): never {
while (true) {}
}
Difference Between void and never
| void | never |
|---|---|
| Function completes normally | Function never completes |
| Returns nothing | Never returns |
Important Interview Point
Interviewers frequently ask:
“Difference between void and never.”
Best answer:
-
void→ no return value -
never→ function never ends or always throws error