Skip to content

Commit

Permalink
feat: add helper function for correct TypeScript typing of yielded Pr…
Browse files Browse the repository at this point in the history
…omises
  • Loading branch information
n1ru4l committed Mar 9, 2020
1 parent a7a3843 commit 3446c24
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 50 deletions.
17 changes: 17 additions & 0 deletions src/use-async-effect.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,20 @@ it("calls latest generator reference upon dependency change", async done => {
unmount();
done();
});

it("interfers the correct type with the typing helper", async done => {
const TestComponent: React.FC<{}> = () => {
useAsyncEffect(function*(setErrorHandler, cast) {
const a = yield* cast(
Promise.resolve({ type: "FOO" as "FOO", value: 1 })
);
expect(a).toEqual({ type: "FOO", value: 1 });
}, []);
return null;
};

const { unmount } = render(<TestComponent />);
await Promise.resolve();
unmount();
done();
});
23 changes: 17 additions & 6 deletions src/use-async-effect.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { useEffect, useRef } from "react";

// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => { };
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};

type GeneratorReturnValueType = void | (() => void);

function* cast<T>(input: Promise<T>): Generator<Promise<T>, T> {
// eslint-disable-next-line
// @ts-ignore
return yield input;
}

type ThenArg<T> = T extends PromiseLike<infer U> ? U : T;

export const useAsyncEffect = (
createGenerator: (
setCancelHandler: (
onCancel?: null | (() => void),
onCancelError?: null | ((err: Error) => void)
) => void
) => // eslint-disable-next-line @typescript-eslint/no-explicit-any
Iterator<any, any, any>,
) => void,
cast: <T>(promise: Promise<T>) => Generator<Promise<T>, T>
) => Iterator<unknown, GeneratorReturnValueType>,
deps: React.DependencyList
) => {
const generatorRef = useRef(createGenerator);
Expand All @@ -27,7 +37,8 @@ export const useAsyncEffect = (
(cancelHandler, cancelErrorHandler) => {
onCancel = cancelHandler || noop;
onCancelError = cancelErrorHandler || noop;
}
},
cast
);
let cleanupHandler = noop;

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"esModuleInterop": true,
"declaration": true,
"skipLibCheck": true,
"downlevelIteration": true,
"lib": ["ESNext", "DOM"]
},
"exclude": ["**/*.spec.ts", "**/*.spec.tsx"]
}
}
44 changes: 2 additions & 42 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2141,7 +2141,7 @@ debug@^3.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -3550,7 +3550,7 @@ import-local@^3.0.2:
pkg-dir "^4.2.0"
resolve-cwd "^3.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -4838,11 +4838,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -4851,33 +4846,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -4918,11 +4891,6 @@ lodash.memoize@4.x:
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down Expand Up @@ -5633,7 +5601,6 @@ npm@^6.10.3:
cmd-shim "^3.0.3"
columnify "~1.5.4"
config-chain "^1.1.12"
debuglog "*"
detect-indent "~5.0.0"
detect-newline "^2.1.0"
dezalgo "~1.0.3"
Expand All @@ -5648,7 +5615,6 @@ npm@^6.10.3:
has-unicode "~2.0.1"
hosted-git-info "^2.8.5"
iferr "^1.0.2"
imurmurhash "*"
infer-owner "^1.0.4"
inflight "~1.0.6"
inherits "^2.0.4"
Expand All @@ -5667,14 +5633,8 @@ npm@^6.10.3:
libnpx "^10.2.0"
lock-verify "^2.1.0"
lockfile "^1.0.4"
lodash._baseindexof "*"
lodash._baseuniq "~4.6.0"
lodash._bindcallback "*"
lodash._cacheindexof "*"
lodash._createcache "*"
lodash._getnative "*"
lodash.clonedeep "~4.5.0"
lodash.restparam "*"
lodash.union "~4.6.0"
lodash.uniq "~4.5.0"
lodash.without "~4.4.0"
Expand Down

0 comments on commit 3446c24

Please sign in to comment.