srmagura / real-cancellable-promise

Cancellable promise library for JavaScript and TypeScript.
https://srmagura.github.io/real-cancellable-promise
MIT License
34 stars 2 forks source link

Fix `capture` function to preserve type identity #4

Closed ondrej-stanek-ozobot closed 1 year ago

ondrej-stanek-ozobot commented 1 year ago

The capture function used in buildCancellablePromise helper function hides the type that is passed to it.

The capture function should act as an identity function from type perspective.

The use-case is that CancellablePromise<T> might be subclassed or enhanced with some additional information, such as progress reporting. We need the enhancements to be preserved when the an enhanced CancellablePromise is passed through the capture function.

Before the fix, the return type from capture would be a bare CancellablePromise<T>, hiding any additional features of the particular type that inherited from CancellablePromise<T> and was passed to capture.

This issue is shown in the automted compile-time test that is part of this PR. The step $ yarn tsc fails when the test is run on the original codebase, but passes after the proposed fix.

Thanks for your work on the real-cancellable-promise package! I am happy I finally found a solid solution to the promise cancellation problem.

ondrej-stanek-ozobot commented 1 year ago

Sure, no problem, please review the fixes.

srmagura commented 1 year ago

Just published 1.1.2 to npm. Thanks for your contribution :)