Promise Types
Promise Types
π¨βπΌ Let's ensure our async functions have proper type annotations. This helps
TypeScript catch errors and makes our code more maintainable.
type Episode = { id: string; title: string }
async function fetchEpisode(): Promise<Episode> {
return { id: 'e1', title: 'Pilot' }
}
async function fetchEpisodeIds(): Promise<Array<string>> {
return ['e1', 'e2']
}
π¨ Open
and:
- Add explicit return type annotations to all async functions
- Use
Promise<T>to type the return values - Ensure the types match what the functions actually return
π¦Ί TypeScript will infer Promise types, but explicit annotations make your
intent clear and help catch errors early.