ReturnType<T> lấy return type của function T mà không cần khai báo lại: type Result = ReturnType<typeof fetchUser> tự động theo dõi khi hàm thay đổi. Awaited<T> unwrap Promise đệ quy: Awaited<Promise<string>> cho ra string, Awaited<Promise<Promise<number>>> cho ra number.
- Kết hợp cả hai cho async functions:
type Data = Awaited<ReturnType<typeof fetchData>>. - Rất hữu ích trong large codebases để tránh type duplication và đảm bảo types luôn đồng bộ với implementation.
ReturnType<T> extracts the return type of function T without re-declaring it: type Result = ReturnType<typeof fetchUser> automatically stays up to date when the function changes. Awaited<T> recursively unwraps Promises: Awaited<Promise<string>> gives string, Awaited<Promise<Promise<number>>> gives number.
- Combine both for async functions:
type Data = Awaited<ReturnType<typeof fetchData>>. - Very useful in large codebases to avoid type duplication and keep types in sync with implementations.