मुख्य सामग्री पर जाएं
eLearner.app
मॉड्यूल 3 · पाठ 2 का 2पाठ्यक्रम में 6/14~15 min
मॉड्यूल पाठ (2/2)

मौलिक जेनेरिक

Generics (generic programming) are one of the most powerful features of TypeScript. They allow you to write flexible, reusable components, functions, and interfaces that can work with different data types while maintaining maximum type safety and avoiding the use of any.


Generic Functions

Think of a generic as a variable for types. Let's consider a function that returns the value passed to it as an argument:

TS
function identity<T>(arg: T): T {
  return arg;
}

The letter T (short for Type) is a placeholder for the type. When we call identity, TypeScript captures the type of the passed argument and automatically sets T to that type:

TS
const str = identity<string>('Hello'); // T is string
const num = identity(42); // T is number (automatically inferred!)

Generic Interfaces and Aliases

Generics can also be applied to interfaces and type aliases to define generic containers or data structures:

TS
interface Box<T> {
  content: T;
}

const stringBox: Box<string> = { content: 'TypeScript' };
const numberBox: Box<number> = { content: 42 };

Multiple Type Parameters

We can define functions or types that use multiple type variables (e.g., T and U):

TS
function mergeObjects<T, U>(obj1: T, obj2: U): T & U {
  return { ...obj1, ...obj2 };
}

Generic Constraints

Sometimes we want to limit the types accepted by a generic. We can use the extends keyword to add a constraint.

For example, if we want the generic type to always have a length property (like strings or arrays):

TS
interface HasLength {
  length: number;
}

function logLength<T extends HasLength>(arg: T): void {
  console.log(arg.length); // Safe! We know arg has 'length'
}

logLength('Hello'); // Valid
logLength([1, 2, 3]); // Valid
// logLength(42); // Error: number does not have a length property!

Try it yourself

Exercise 1: Generic Identity Function

व्यायाम#ts.m3.l2.e1
प्रयास: 0लोड हो रहा है...

Complete the generic identity function so that it accepts a value parameter of generic type T and returns it without modification.

संपादक लोड हो रहा है...
संकेत दिखाएँ

The return type must be the same generic type T as the parameter.

3 प्रयासों के बाद समाधान उपलब्ध है

Exercise 2: Generic Box Wrapper

व्यायाम#ts.m3.l2.e2
प्रयास: 0लोड हो रहा है...

Define a generic interface named Box that accepts a type parameter T and has a single property named content of type T.

संपादक लोड हो रहा है...
संकेत दिखाएँ

Use the interface Box<T> { content: T; } syntax.

3 प्रयासों के बाद समाधान उपलब्ध है

Exercise 3: Generic Constraint

व्यायाम#ts.m3.l2.e3
प्रयास: 0लोड हो रहा है...

Write a generic function named getLength that accepts a parameter arg. arg must be constrained to an interface that has a length property (number). The function must return arg.length.

संपादक लोड हो रहा है...
संकेत दिखाएँ

Declare the function as getLength<T extends HasLength>(arg: T): number and return arg.length.

3 प्रयासों के बाद समाधान उपलब्ध है

Exercise 4: Generic Pair

व्यायाम#ts.m3.l2.e4
प्रयास: 0लोड हो रहा है...

Define a generic interface Pair<T, U> with two properties: first of type T and second of type U. Next, create a generic function makePair that accepts first (of type T) and second (of type U) and returns an object implementing Pair<T, U>.

संपादक लोड हो रहा है...
संकेत दिखाएँ

Use the syntax for two generic parameters Pair<T, U> and define makePair accordingly.

3 प्रयासों के बाद समाधान उपलब्ध है