/** * Extracts the resolved type of a Promise or the return type of a non-Promise function. * Useful for inferring the return type of async functions. */ type AsyncReturnType any> = Awaited>; // Example 1: Async function async function fetchData(id: number): Promise<{ id: number; data: string }> { return { id, data: `Data for ${id}` }; } type DataResult = AsyncReturnType; // type DataResult = { id: number; data: string; } const result: DataResult = await fetchData(1); console.log(result.data); // 'Data for 1' // Example 2: Regular function (still works) function getNumber(): number { return 42; } type NumberResult = AsyncReturnType; // type NumberResult = number const numResult: NumberResult = getNumber(); console.log(numResult); // 42 // Example 3: Function returning a Promise (explicitly) function fetchUser(userId: string): Promise<{ userId: string; name: string }> { return Promise.resolve({ userId, name: 'John Doe' }); } type UserResult = AsyncReturnType; // type UserResult = { userId: string; name: string; } const userResult: UserResult = await fetchUser('u123'); console.log(userResult.name); // 'John Doe'