Utility Types में Parameters
In this page:
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
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
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
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
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
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"));
Parameters<createUser>लिखना बजायParameters<typeof createUser>के।- named keys वाले एक object की उम्मीद करना, जब
Parametersएक tuple return करता है, तो आपargs[0]access करते हैं। - Tuple में मौजूद values से कम या ज़्यादा assign करना, जैसे
["Ravi"]को[string, number]के लिए, जो एक error है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- TypeScript Advanced Types
- Mapped Types
- Conditional Types
- Custom Type Guards
- Assertion Functions
- Control Flow Analysis
- Exhaustiveness Checking
- Satisfies Operator
- Template Literal Types
- Utility Types - Partial
- Utility Types - Required
- Utility Types - Readonly
- Utility Types - Pick
- Utility Types - Omit
- Utility Types - Record
- Utility Types - Exclude and Extract
- Utility Types - NonNullable
- Utility Types - ReturnType
- Utility Types - Parameters
- TypeScript 5 Updates