I have been trying to implement an auto-commit process with support for thenables. However, I am facing difficulty resolving a type error that was raised during the last attempt:
type JobResolve = () => void
export class Job implements PromiseLike<JobResolve> {
public then<TResult1 = JobResolve>(onfulfilled?: ((value: JobResolve) => (PromiseLike<TResult1> | TResult1)) | undefined | null): PromiseLike<TResult1> {
const done = () => {}
if (typeof onfulfilled === 'function') {
const thenable = Promise.resolve(onfulfilled(done))
done()
return thenable
} else {
// Error occurs here!
// TS2322: '() => void' is assignable to the constraint of type 'TResult1', but 'TResult1' could be instantiated with a different subtype of constraint '{}'.
return Promise.resolve(done)
}
}
}
Any suggestions on how to resolve this issue without using Promise.resolve(done as any)
?