Implement MyParameters<T> using conditional types and infer
Tests if you can extract parameter types with infer. A strong answer matches T against a callable signature, uses infer to bind parameters into a tuple, and defaults to never for non-functions. Red flag: using any or indexing without inference.
WHAT THIS TESTS:
This question tests your understanding of conditional types and type inference in TypeScript's type system. Specifically, it checks whether you know how to use the extends keyword to pattern-match against a function shape, and how infer creates a type variable that captures a piece of that shape without explicit generic parameters. It also reveals whether you understand deferred type resolution, where the compiler waits until the checked type is concrete before resolving the inferred variable. Senior candidates should also mention that infer can only appear in the extends clause of a conditional type.
A GOOD ANSWER COVERS:
First, the exact type definition: type MyParameters<T> = T extends (...args: infer P) => any ? P : never. Second, an explanation that infer P declares a type variable inside the extends clause that binds to the parameters tuple only when T is assignable to a function type. Third, the fallback branch never, which prevents non-function types from producing a valid tuple and maintains strictness. Fourth, a note on deferred resolution: the conditional type is not evaluated immediately for a generic T, but rather distributed or resolved once T is known at the call site, making the utility lazy.
COMMON WRONG ANSWERS:
A common mistake is writing T extends Function ? T["length"] or trying to index T without inference, which fails because the global Function type has no callable parameter tuple. Another red flag is omitting the never fallback, which can cause unexpected assignability issues when MyParameters is used with union types or non-functions. Some candidates treat infer as a generic parameter, writing <infer P> or similar, which is a syntax error. Others use any as the return type in the function pattern but forget that the pattern itself must be a valid function type signature for the match to succeed.
LIKELY FOLLOW-UPS:
An interviewer might ask what happens with overloaded functions, in which case MyParameters would typically infer the parameters from the last overload signature. They might also ask about the distributive behavior of conditional types over unions, leading to MyParameters<(() => void) | ((x: string) => void)> producing never | [x: string]. Another follow-up is implementing ReturnType<T> by inferring the return position instead of the parameters, or asking why the return type in the pattern is any rather than unknown.
ONE CONCRETE EXAMPLE:
Consider type Fn = (name: string, age: number) => void. Applying MyParameters<Fn> resolves as follows: Fn extends (...args: infer P) => any is true, so P is bound to [name: string, age: number]. The result is the tuple [name: string, age: number]. If you instead pass a non-function like string, the condition fails and the result is never, preserving type safety and preventing invalid usage downstream.
Read the original → typescriptlang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.