Promise<T>
Promise<T> is the generic interface for a thenable that fulfills with T. T is the success value. Rejections are not modeled as a second type parameter.
Detailed Explanation
The TypeScript lib defines something equivalent to:
interface Promise<T> {
then<TResult1 = T, TResult2 = never>(
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
): Promise<TResult1 | TResult2>;
}
T appears in the fulfillment callback. The rejection callback takes any (or unknown in some stricter libs). There is no Promise<T, E>. Errors are untyped in the standard. If you want a typed failure, return a Result<T, E> inside the promise (Promise<Result<T, E>>) or use a library. Do not pretend catch is generic over E.
PromiseLike<T> is the smaller thenable interface: anything with a then. await and Promise.resolve accept PromiseLike. Your own types should usually return Promise<T> so callers get .catch and the rest of the spec methods.
T can be another promise. Promise<Promise<User>> is a type you can write and almost never want; async/await and Promise.resolve flatten, and Awaited<T> unwraps. If you see a nested Promise in a hover, you probably returned a promise from a function that was already async.
new Promise<T>((resolve, reject) => { ... }) asks you to call resolve with T. Forgetting the type argument infers T from resolve calls, or as unknown. Annotate new Promise<User> when the executor is long.
void vs undefined shows up here. A function typed (): Promise<void> may resolve with undefined. Callers should not use the value. Promise<undefined> means the value is undefined and they may.
Because T is erased, a Promise<User> can fulfill with anything if the implementation asserted. The constructor and async return checks are the main guards. At the boundary (fetch, a worker message, an ORM), validate and then resolve with the checked T.
Code Examples
1. The generic is the fulfillment value.
// 1. The generic is the fulfillment value.
type User = { id: string };
function loadUser(id: string): Promise<User> {
return new Promise((resolve) => {
resolve({ id });
});
}
loadUser("u1").then((user) => {
console.log(user.id);
});2. There is no E — encode failure in T if you need it.
// 2. There is no E — encode failure in T if you need it.
type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
function findUser(id: string): Promise<Result<User, "missing">> {
if (id === "") return Promise.resolve({ ok: false, error: "missing" });
return Promise.resolve({ ok: true, value: { id } });
}3. Awaited unwraps nested promises.
// 3. Awaited unwraps nested promises.
type Nested = Promise<Promise<User>>;
type Flat = Awaited<Nested>; // UserCommon Gotchas
Rejections are not `E`
`Promise<User>` can still reject with a string, an `Error`, or anything. Type the `catch` binding as `unknown`.
`new Promise` without `T` and without a clear `resolve` argument infers poorly
write `new Promise<User>`.
`Promise<void>` means "ignore the value"
do not use it as a substitute for "I forgot to return."