← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 19 of 20

Utility Types में Parameters

Parameters<T> एक function के parameter types को एक tuple के रूप में extract करता है। यह उपयोगी है जब दूसरे type या function को एक existing function की argument structure reuse करनी हो।
Syntax
typescript
type Result = Parameters<typeof functionName>;

Basic Parameters

Parameters<T> एक tuple type return करता है जिसमें एक function type T के parameters के types होते हैं, उसी order में जिसमें वे function की खुद की signature में आते हैं।

यह ऐसे wrapper या higher-order functions बनाने के सबसे common utility types में से एक है जो एक existing signature mirror करते हैं।

उदाहरण: Basic Parameters

typescript
function createUser(name: string, age: number) {
  return { name, age };
}
type CreateUserArgs = Parameters<typeof createUser>;
const args: CreateUserArgs = ["Ravi", 25];
console.log(createUser(...args));

Parameters with Arrow Functions

Parameters arrow functions के साथ उसी तरह काम करता है जैसे यह regular function declarations के साथ करता है, क्योंकि दोनों अंदर से एक ही तरह का function type produce करते हैं।

उदाहरण: Parameters with Arrow Functions

typescript
const add = (a: number, b: number) => a + b;
type AddArgs = Parameters<typeof add>;
const args: AddArgs = [2, 3];
console.log(add(...args));

Parameters with Optional Arguments

Parameters resulting tuple में optional parameters preserve करता है, corresponding tuple positions को optional मार्क करते हुए बिल्कुल जैसे वे original function signature में थे।

उदाहरण: Parameters with Optional Arguments

typescript
function greet(name: string, greeting?: string) {
  return `${greeting ?? "Hello"}, ${name}`;
}
type GreetArgs = Parameters<typeof greet>;
const args: GreetArgs = ["Ravi"];
console.log(greet(...args));

Parameters with Rest Arguments

Parameters rest parameters को resulting tuple या array structure के हिस्से के रूप में extract कर सकता है, एक variadic function की असली calling convention reflect करते हुए, किसी भी trailing array-typed rest argument सहित।

उदाहरण: Parameters with Rest Arguments

typescript
function sum(first: number, ...rest: number[]) {
  return first + rest.reduce((a, b) => a + b, 0);
}
type SumArgs = Parameters<typeof sum>;
const args: SumArgs = [1, 2, 3];
console.log(sum(...args));

When to Use Parameters

Parameters उपयोग करें जब एक type को एक existing function की argument list के साथ synchronized रहना चाहिए, जैसे एक wrapper या mock बनाना जो हमेशा original function की signature से match करे।

उदाहरण: When to Use Parameters

typescript
function original(a: number, b: string) {
  return `${a}-${b}`;
}
function wrapper(...args: Parameters<typeof original>) {
  console.log("Calling with", args);
  return original(...args);
}
console.log(wrapper(1, "x"));
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Parameters<createUser> लिखना बजाय Parameters<typeof createUser> के।
  2. named keys वाले एक object की उम्मीद करना, जब Parameters एक tuple return करता है, तो आप args[0] access करते हैं।
  3. Tuple में मौजूद values से कम या ज़्यादा assign करना, जैसे ["Ravi"] को [string, number] के लिए, जो एक error है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.