In my code, I am creating a let
variable named resolver
which I intend to set within a promise constructor function.
interface Request {
ids: string[];
resolver: () => void;
promise: Promise<unknown>
}
class Foo {
public requests: Request[] = [];
public createPromise = (ids: string[]) => {
let resolver;
const promise = new Promise((r) => { resolver = r; });
this.requests.push({ ids, resolver, promise });
return promise;
};
}
However, when trying to add the object to the requests list, TypeScript throws an error regarding the resolver
:
Type 'undefined' is not assignable to type '() => void'.ts(2322)
This means that the resolver
variable inside the promise constructor is interpreted as undefined
.