Skip to content

Commit 3446c24

Browse files
committed
feat: add helper function for correct TypeScript typing of yielded Promises
1 parent a7a3843 commit 3446c24

File tree

4 files changed

+38
-50
lines changed

4 files changed

+38
-50
lines changed

src/use-async-effect.spec.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,20 @@ it("calls latest generator reference upon dependency change", async done => {
391391
unmount();
392392
done();
393393
});
394+
395+
it("interfers the correct type with the typing helper", async done => {
396+
const TestComponent: React.FC<{}> = () => {
397+
useAsyncEffect(function*(setErrorHandler, cast) {
398+
const a = yield* cast(
399+
Promise.resolve({ type: "FOO" as "FOO", value: 1 })
400+
);
401+
expect(a).toEqual({ type: "FOO", value: 1 });
402+
}, []);
403+
return null;
404+
};
405+
406+
const { unmount } = render(<TestComponent />);
407+
await Promise.resolve();
408+
unmount();
409+
done();
410+
});

src/use-async-effect.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import { useEffect, useRef } from "react";
22

3-
// eslint-disable-next-line @typescript-eslint/no-empty-function
4-
const noop = () => { };
3+
// eslint-disable-next-line @typescript-eslint/no-empty-function
4+
const noop = () => {};
5+
6+
type GeneratorReturnValueType = void | (() => void);
7+
8+
function* cast<T>(input: Promise<T>): Generator<Promise<T>, T> {
9+
// eslint-disable-next-line
10+
// @ts-ignore
11+
return yield input;
12+
}
13+
14+
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T;
515

616
export const useAsyncEffect = (
717
createGenerator: (
818
setCancelHandler: (
919
onCancel?: null | (() => void),
1020
onCancelError?: null | ((err: Error) => void)
11-
) => void
12-
) => // eslint-disable-next-line @typescript-eslint/no-explicit-any
13-
Iterator<any, any, any>,
21+
) => void,
22+
cast: <T>(promise: Promise<T>) => Generator<Promise<T>, T>
23+
) => Iterator<unknown, GeneratorReturnValueType>,
1424
deps: React.DependencyList
1525
) => {
1626
const generatorRef = useRef(createGenerator);
@@ -27,7 +37,8 @@ export const useAsyncEffect = (
2737
(cancelHandler, cancelErrorHandler) => {
2838
onCancel = cancelHandler || noop;
2939
onCancelError = cancelErrorHandler || noop;
30-
}
40+
},
41+
cast
3142
);
3243
let cleanupHandler = noop;
3344

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"esModuleInterop": true,
55
"declaration": true,
66
"skipLibCheck": true,
7+
"downlevelIteration": true,
78
"lib": ["ESNext", "DOM"]
8-
},
9-
"exclude": ["**/*.spec.ts", "**/*.spec.tsx"]
9+
}
1010
}

yarn.lock

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,7 @@ debug@^3.1.0:
21412141
dependencies:
21422142
ms "^2.1.1"
21432143

2144-
debuglog@*, debuglog@^1.0.1:
2144+
debuglog@^1.0.1:
21452145
version "1.0.1"
21462146
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
21472147
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -3550,7 +3550,7 @@ import-local@^3.0.2:
35503550
pkg-dir "^4.2.0"
35513551
resolve-cwd "^3.0.0"
35523552

3553-
imurmurhash@*, imurmurhash@^0.1.4:
3553+
imurmurhash@^0.1.4:
35543554
version "0.1.4"
35553555
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
35563556
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -4838,11 +4838,6 @@ lockfile@^1.0.4:
48384838
dependencies:
48394839
signal-exit "^3.0.2"
48404840

4841-
lodash._baseindexof@*:
4842-
version "3.1.0"
4843-
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
4844-
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
4845-
48464841
lodash._baseuniq@~4.6.0:
48474842
version "4.6.0"
48484843
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -4851,33 +4846,11 @@ lodash._baseuniq@~4.6.0:
48514846
lodash._createset "~4.0.0"
48524847
lodash._root "~3.0.0"
48534848

4854-
lodash._bindcallback@*:
4855-
version "3.0.1"
4856-
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
4857-
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
4858-
4859-
lodash._cacheindexof@*:
4860-
version "3.0.2"
4861-
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
4862-
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
4863-
4864-
lodash._createcache@*:
4865-
version "3.1.2"
4866-
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
4867-
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
4868-
dependencies:
4869-
lodash._getnative "^3.0.0"
4870-
48714849
lodash._createset@~4.0.0:
48724850
version "4.0.3"
48734851
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
48744852
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
48754853

4876-
lodash._getnative@*, lodash._getnative@^3.0.0:
4877-
version "3.9.1"
4878-
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
4879-
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
4880-
48814854
lodash._root@~3.0.0:
48824855
version "3.0.1"
48834856
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
@@ -4918,11 +4891,6 @@ lodash.memoize@4.x:
49184891
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
49194892
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
49204893

4921-
lodash.restparam@*:
4922-
version "3.6.1"
4923-
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
4924-
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
4925-
49264894
lodash.set@^4.3.2:
49274895
version "4.3.2"
49284896
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
@@ -5633,7 +5601,6 @@ npm@^6.10.3:
56335601
cmd-shim "^3.0.3"
56345602
columnify "~1.5.4"
56355603
config-chain "^1.1.12"
5636-
debuglog "*"
56375604
detect-indent "~5.0.0"
56385605
detect-newline "^2.1.0"
56395606
dezalgo "~1.0.3"
@@ -5648,7 +5615,6 @@ npm@^6.10.3:
56485615
has-unicode "~2.0.1"
56495616
hosted-git-info "^2.8.5"
56505617
iferr "^1.0.2"
5651-
imurmurhash "*"
56525618
infer-owner "^1.0.4"
56535619
inflight "~1.0.6"
56545620
inherits "^2.0.4"
@@ -5667,14 +5633,8 @@ npm@^6.10.3:
56675633
libnpx "^10.2.0"
56685634
lock-verify "^2.1.0"
56695635
lockfile "^1.0.4"
5670-
lodash._baseindexof "*"
56715636
lodash._baseuniq "~4.6.0"
5672-
lodash._bindcallback "*"
5673-
lodash._cacheindexof "*"
5674-
lodash._createcache "*"
5675-
lodash._getnative "*"
56765637
lodash.clonedeep "~4.5.0"
5677-
lodash.restparam "*"
56785638
lodash.union "~4.6.0"
56795639
lodash.uniq "~4.5.0"
56805640
lodash.without "~4.4.0"

0 commit comments

Comments
 (0)