tezvyn:

Create a User type alias from a function return type

AI-drafted, machine-checkedSource: typescriptlang.orgbeginner
WHAT IT TESTS

TypeScript type-space introspection.

ANSWER OUTLINE

Use typeof to grab the function type, then ReturnType to extract the return object shape.

RED FLAG

Manually copying the object literal or confusing value-space and type-space typeof.

WHAT THIS TESTS: This question evaluates your grasp of TypeScript type-space introspection and your instinct to avoid manual type duplication. The interviewer wants to see that you treat types as derivable artifacts rather than independent declarations that must be maintained by hand. Knowing how to extract a return type signals familiarity with the standard library utility types and the typeof type operator, both of which are fundamental to writing scalable TypeScript.

A GOOD ANSWER COVERS: A strong answer walks through two precise steps. First, use the typeof operator in type space to reference the function type itself, writing typeof createUser. This is distinct from JavaScript's runtime typeof expression. Second, feed that function type into the ReturnType utility type, which is defined in lib.es5.d.ts as a conditional type that infers the return position. The complete alias is type User = ReturnType<typeof createUser>. Excellent candidates add that this preserves exact modifiers such as readonly or optional properties and explain that the alias stays in sync if the function signature changes.

COMMON WRONG ANSWERS: The most common red flag is manually transcribing the object literal into an interface or type alias. That approach creates a maintenance burden and risks drift when the function is refactored. Another error is attempting to call the function in type space, such as typeof createUser(), which is syntactically invalid. Some candidates reach for Parameters<typeof createUser> and try to index into a nonexistent return tuple, showing confusion about the utility types. Finally, treating typeof as a runtime value check rather than a type query operator reveals a fundamental misunderstanding of TypeScript's two-space model.

LIKELY FOLLOW-UPS: Interviewers often probe deeper by asking how ReturnType is implemented under the hood, expecting you to describe a conditional type with infer R in the return position. They may ask about overloaded functions, where ReturnType resolves to the last overload's return type. Another follow-up is asking you to transform the extracted type further, for example making all properties optional with Partial or immutable with Readonly, which tests composition of utility types.

ONE CONCRETE EXAMPLE: Consider a createInvoice function that returns an object with twenty fields including nested line items, tax calculations, and ISO timestamps. Maintaining a separate Invoice interface by hand is brittle. Instead, declaring type Invoice = ReturnType<typeof createInvoice> guarantees that any change to the function's return shape, such as adding a discount field, immediately propagates to every consumer. If the team later decides to wrap the return in a Promise, you can update the alias to Awaited<ReturnType<typeof createInvoice>> with a single edit.

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.