ReturnType<typeof createCart> is the type createCart() returns. When a factory function defines an object's shape, this names that shape for other functions' @param tags without repeating it, as Cart in the first example.
ReturnType takes a function type, so the usual spelling puts typeof in front of the function's name. TypeScript added it in version 2.8.
At a glance
| JSDoc in a .js file | TypeScript in a .ts file |
|---|---|
/** @type {ReturnType<typeof createCart>} */ | : ReturnType<typeof createCart> |
Examples
export function createCart() {
return {
/** @type {{ sku: string, quantity: number }[]} */
lines: [],
coupon: /** @type {string | null} */ (null),
};
}
/** @typedef {ReturnType<typeof createCart>} Cart */
/** @param {Cart} cart */
export function itemCount(cart) {
return cart.lines.reduce((n, line) => n + line.quantity, 0);
}
itemCount(createCart());export function createCart() {
return {
lines: [] as { sku: string; quantity: number }[],
coupon: null as string | null,
};
}
type Cart = ReturnType<typeof createCart>;
export function itemCount(cart: Cart) {
return cart.lines.reduce((n, line) => n + line.quantity, 0);
}
itemCount(createCart());jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": []
},
"include": ["*.js"]
} export function createCart() {
return { lines: /** @type {string[]} */ ([]) };
}
// TypeScript resolves a value name in JSDoc to the value's type, so this passes in a .js file.
/** @type {ReturnType<createCart>} */
const cart = createCart();
export { cart };export function createCart() {
return { lines: [] as string[] };
}
const cart: ReturnType<createCart> = createCart();error TS2749: 'createCart' refers to a value, but is being used as a type here. Did you mean 'typeof createCart'?
export { cart };jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": []
},
"include": ["*.js"]
} Without typeof in a .js file
In a .js file TypeScript resolves a value's name in a JSDoc type to the value's type, so ReturnType<createCart> works there too. The same line in a .ts file is TS2749 ("'createCart' refers to a value, but is being used as a type here. Did you mean 'typeof createCart'?"); the second example shows both tabs. Write typeof so the type means the same in both kinds of file.
Async functions
For an async function, ReturnType is the Promise. Wrap it in Awaited for the resolved value: Awaited<ReturnType<typeof loadAccount>>.
Under TypeScript 6.0 and 7.0
Every example on this page also runs under TypeScript 6.0.3 and 7.0.2 when this site is built. One of them reports something different from 5.9.3, the version the page describes; the ✗ on its badge links to its entry below.
A value's name is not a type
The pattern: {ReturnType<createCart>} or {Status}, where createCart or Status is a value, not a type.
Why: 5.9 read a value's name in a JSDoc type as typeof that value, in .js files only. The port's list of changes says values are no longer resolved as types in JSDoc, so the .js file now reports TS2749, as its .ts twin always did. Source: CHANGES.md: Values are no longer resolved as types in JSDoc type positions.
In a .js file a function name also works as a type; in .ts it needs typeof cart.js
- TypeScript 5.9.3
cart.ts(6,24): error TS2749: 'createCart' refers to a value, but is being used as a type here. Did you mean 'typeof createCart'?
- TypeScript 6.0.3same as 5.9.3
The same errors as 5.9.3.
- TypeScript 7.0.2differs from 5.9.3
cart.js(6,23): error TS2749: 'createCart' refers to a value, but is being used as a type here. Did you mean 'typeof createCart'? cart.ts(6,24): error TS2749: 'createCart' refers to a value, but is being used as a type here. Did you mean 'typeof createCart'?
What works in every version: Write typeof: {ReturnType<typeof createCart>}.
The example ReturnType of a function, through typeof on this page shows it, with the same result under every version.
Gotchas
- For an overloaded function,
ReturnTypeuses the last overload. - The factory's return type must be clear to TypeScript; empty arrays and
nullinitial values in the returned object need a@typecomment or a cast, as in the first example, or TypeScript types them asnever[]andnull, and nothing else can be stored in them. - TypeScript 7.0 no longer accepts a value's name as a type:
ReturnType<createCart>is TS2749 there (the section Under TypeScript 6.0 and 7.0), so writeReturnType<typeof createCart>.
Questions
How do I get a function's return type in JSDoc?
{ReturnType<typeof yourFunction>}. For an async function, {Awaited<ReturnType<typeof yourFunction>>}.
Sources
- Utility Types: ReturnType<Type> · TypeScript handbook
- TypeScript 2.8: predefined conditional types · TypeScript release notes
Last reviewed 21 October 2026.