Building ReturnType and Parameters from Scratch
Recreate built-in utility types using infer.
Building ReturnType and Parameters from Scratch is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Goal: Recreate Built-in Utilities
TypeScript ships ReturnType and Parameters as built-ins. Rebuilding them from scratch cements your understanding of infer and conditional types.
// Built-in versions (for reference)
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;Step 1: MyReturnType
We constrain T to any function, match the return position with infer R, and return R on match.
type MyReturnType<T extends (...args: any[]) => any> =
T extends (...args: any[]) => infer R ? R : never;
type A = MyReturnType<() => string>; // string
type B = MyReturnType<(x: number) => boolean>; // booleanStep 2: MyParameters
We capture the parameter tuple using infer P on the argument list position.
type MyParameters<T extends (...args: any[]) => any> =
T extends (...args: infer P) => any ? P : never;
type C = MyParameters<(a: string, b: number) => void>;
// [a: string, b: number]Step 3: FirstParameter
Extract only the first parameter by deconstructing the inferred tuple.
type FirstParameter<T extends (...args: any[]) => any> =
T extends (first: infer F, ...rest: any[]) => any ? F : never;
type D = FirstParameter<(name: string, age: number) => void>; // stringStep 4: ConstructorParameters
For class constructors, use new (...args: any[]) => any as the constraint and pattern.
type MyConstructorParameters<T extends new (...args: any[]) => any> =
T extends new (...args: infer P) => any ? P : never;
class User { constructor(name: string, age: number) {} }
type E = MyConstructorParameters<typeof User>; // [name: string, age: number]Step 5: InstanceType
Capture the constructed instance type with infer I on the return position of the constructor signature.
type MyInstanceType<T extends new (...args: any[]) => any> =
T extends new (...args: any[]) => infer I ? I : never;
type F = MyInstanceType<typeof User>; // UserCombining ReturnType and Parameters
You can compose these utilities to describe function shapes precisely.
function add(a: number, b: number): number { return a + b; }
type AddParams = MyParameters<typeof add>; // [a: number, b: number]
type AddReturn = MyReturnType<typeof add>; // numberWhy any[] in the Constraint?
Using (...args: any[]) => any instead of (...args: unknown[]) => unknown is necessary because any[] is bivariant in function positions, allowing any function to satisfy the constraint.
// unknown[] would reject some valid functions
type Strict<T extends (...args: unknown[]) => unknown> = // too strict
T extends (...args: infer P) => infer R ? [P, R] : never;Labeled Tuple Preservation
TypeScript preserves parameter labels in the inferred tuple, making Parameters output more readable.
type G = MyParameters<(name: string, age: number) => void>;
// [name: string, age: number] — labels preservedAsync Functions
For async functions, MyReturnType returns Promise. Wrap with Awaited to unwrap.
async function fetchUser(): Promise<User> { /* ... */ return new User("A", 1); }
type H = MyReturnType<typeof fetchUser>; // Promise<User>
type I = Awaited<MyReturnType<typeof fetchUser>>; // UserRecap: Building Utilities
Rebuilding ReturnType and Parameters from scratch shows that infer captures type positions structurally. The same pattern applies to constructors, first arguments, and async wrappers.
Quick Check
What does Parameters return?
What You Learned
You rebuilt ReturnType, Parameters, ConstructorParameters, and InstanceType from scratch using infer. This pattern — constrain, match, infer — is the core of type-level metaprogramming in TypeScript.
Frequently asked questions
Is the “Building ReturnType and Parameters from Scratch” lesson free?
Yes — the full text of “Building ReturnType and Parameters from Scratch” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building ReturnType and Parameters from Scratch”?
Recreate built-in utility types using infer. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building ReturnType and Parameters from Scratch” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding infer in Conditional Types
- Building ReturnType and Parameters from Scratch
- Deeply Nested Inference Patterns
- Practical infer Use Cases: Unwrapping Promises