Skip to content
tezvyn:
Menu

Create a User type alias from a function return type

Source: typescriptlang.orgEasyHow cards are made

Summary

TypeScript type-space introspection.

Key points

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

Watch out for

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

What's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

Which TypeScript declaration derives a User type alias from a createUser function and stays synchronized with return-type changes?

  • a.type User = typeof createUser
  • b.type User = ReturnType<typeof createUser()>
  • c.type User = ReturnType<createUser>
  • d.type User = ReturnType<typeof createUser>Correct
Why?

ReturnType<typeof createUser> extracts the return shape in type space so the alias stays in sync with the function. Omitting typeof, as in option C, treats createUser as a value rather than a type query and causes a compile error.

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles