ts-reset
ts-reset copied to clipboard
Reset `AbortSignal`'s `reason` to `unknown`
AbortController is JavaScript's built-in cancelation interface for asynchronous requests (like fetch).
When aborting, you can provide a "reason" - Typescript types reason as any:
const controller = new AbortController();
const signal = controller.signal;
const fetchData = async () => {
try {
const response = await fetch("some-url", { signal });
// ...
} catch (e) {
if (e instanceof Error && e.name === "AbortError") {
console.log(signal.reason.toUpperCase()); // `signal.reason` is `any`, so no complains here
}
}
};
fetchData();
controller.abort(3); // This will throw a TypeError because I'm calling toUpperCase
It seems like "resetting" AbortSignal's reason to unknown is a useful change that is in the spirit of this library.
I'd be glad to open a PR.
Yep, makes sense!