*** 1. Is the Following TypeScript Code Valid? Explain Why.

This question tests understanding of TypeScript type checking and compile-time safety.

Example

let age: number = "25";

❌ This code is invalid.

Why?

The variable age is declared with type number, but a string value is assigned.

TypeScript performs static type checking, so incompatible assignments are detected during compilation.

Correct Version

let age: number = 25;

Important Interview Points

  • TypeScript is statically typed.
  • Type mismatch causes compile-time errors.
  • Improves code safety and maintainability.

*** 2. Explain Why a Specific TypeScript Snippet is Marked Wrong.

Interviewers often provide incorrect code snippets to test debugging and type analysis skills.

Example

function add(a: number, b: number): string {
return a + b;
}

❌ Incorrect because the function return type is string, but the function returns a number.

Correct Version

function add(a: number, b: number): number {
return a + b;
}

OR

function add(a: number, b: number): string {
return (a + b).toString();
}

Important Interview Points

Always verify:

  • Return types
  • Parameter types
  • Variable assignments
  • Null and undefined handling

*** 3. What is Wrong with function reverse(s: String): String;?

function reverse(s: String): String;

Problems

1. Uses String Instead of string

String is an object wrapper type.

TypeScript recommends using primitive types:

string
number
boolean

instead of:

String
Number
Boolean

2. Missing Function Implementation

The statement only declares a function signature.

Correct Version

function reverse(s: string): string {
return s.split("").reverse().join("");
}

Important Interview Points

Frequently asked topic:

  • Primitive types vs wrapper object types
  • string vs String
  • number vs Number

** 4. What are Advanced Uses of Decorators?

Decorators are special annotations used to modify classes, methods, properties, or parameters.

Decorators are heavily used in frameworks like:

  • Angular
  • NestJS

Advanced Uses of Decorators

1. Dependency Injection

@Injectable()
class UserService {}

2. Logging

function Log(target: any, key: string) {
console.log(`${key} called`);
}

3. Authorization

@Role("admin")

4. Validation

@Required
name: string;

5. Metadata Reflection

Used in enterprise frameworks and backend architectures.

Types of Decorators

  • Class Decorators
  • Method Decorators
  • Property Decorators
  • Parameter Decorators

Important Interview Points

Interviewers may ask:

  • Decorator execution order
  • Experimental decorator support
  • Real-world framework usage

*** 5. Explain Advanced Type Assertions.

Type assertions tell TypeScript to treat a value as a specific type.

Syntax

value as Type

OR

<Type>value

Example

let value: unknown = "Hello";

let length = (value as string).length;

Advanced Type Assertions

1. DOM Assertions

const input = document.getElementById("name") as HTMLInputElement;

2. Double Assertions

let value: unknown = "Hello";

let num = value as unknown as number;

Used carefully for incompatible type conversions.

3. Non-Null Assertion

document.getElementById("app")!;

Important Interview Points

  • Type assertions do not change runtime behavior.
  • They only help the compiler understand types.
  • Overusing assertions reduces type safety.

*** 6. What are Advanced Generic Constraints?

Generic constraints restrict which types can be used with generics.

Basic Constraint

function printLength<T extends { length: number }>(arg: T): void {
console.log(arg.length);
}

Only values containing a length property are allowed.

Using Interfaces

interface Person {
name: string;
}

function greet<T extends Person>(obj: T) {
console.log(obj.name);
}

Using keyof

function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}

Benefits

  • Better type safety
  • Reusable code
  • Improved IntelliSense support

Important Interview Points

Frequently asked concepts:

  • extends
  • keyof
  • Generic utility patterns

*** 7. How Does TypeScript Handle Structural Typing?

TypeScript uses structural typing, also called duck typing.

Type compatibility depends on object structure, not inheritance.

Example

interface User {
name: string;
}

class Person {
name: string = "Jitendra";
}

let user: User = new Person();

✅ Valid because Person contains the required structure.

Key Idea

“If an object has the required properties, it is compatible.”

Advantages

  • Flexible development
  • Easier object compatibility
  • Better reusable code

Important Interview Points

TypeScript differs from languages like:

  • Java
  • C#

which mainly use nominal typing.


*** 8. What are Discriminated Unions in TypeScript?

Discriminated unions are unions with a common literal property used for type narrowing.

Example

interface Circle {
kind: "circle";
radius: number;
}

interface Square {
kind: "square";
side: number;
}

type Shape = Circle | Square;

Usage

function area(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
}

return shape.side * shape.side;
}

Advantages

  • Better type safety
  • Cleaner conditional logic
  • Smart IntelliSense

Important Interview Points

Very common in:

  • React interviews
  • Frontend architecture interviews
  • Advanced TypeScript interviews

*** 9. What are Intersection Types in TypeScript?

Intersection types combine multiple types into a single type.

Syntax

type A & B

Example

type Employee = {
name: string;
};

type Manager = {
department: string;
};

type TeamLead = Employee & Manager;

Result

{
name: string;
department: string;
}

Common Uses

  • Combining APIs
  • Extending models
  • Reusable type composition

Important Interview Points

Difference between:

  • Union (|) → Either type
  • Intersection (&) → Combined type

is frequently asked.


*** 10. Explain Mapped Types in TypeScript.

Mapped types create new types by transforming existing types.

Example

type ReadOnly<T> = {
readonly [K in keyof T]: T[K];
};

Usage

interface User {
name: string;
age: number;
}

type ReadOnlyUser = ReadOnly<User>;

Common Built-in Mapped Types

Partial

Partial<User>

Makes all properties optional.

Required

Required<User>

Makes all properties required.

Readonly

Readonly<User>

Makes properties immutable.

Pick

Pick<User, "name">

Selects specific properties.

Omit

Omit<User, "age">

Removes specific properties.

Important Interview Points

Mapped types are heavily used in:

  • Enterprise TypeScript applications
  • React props handling
  • API transformations
  • Utility type creation