You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -225,6 +212,43 @@ Add the following to your eslint config file:
225
212
}
226
213
```
227
214
215
+
#### Usage with TypeScript
216
+
217
+
Unfortunately, it is currently not possible to [to interfer the type of a yield expression based on the yielded value](https://github.com/microsoft/TypeScript/issues/32523).
218
+
However, there is a workaround for typing yielded results.
219
+
220
+
```tsx
221
+
useAsyncEffect(function*() {
222
+
// without the type annotation `numericValue` would be of the type `any`
For complex use cases you can leverage some TypeScript utility types ([based on Conditional Types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types)):
Or the "shorter version" (less variable assignments):
239
+
240
+
```tsx
241
+
typeThenArg<T> =TextendsPromiseLike<inferU> ?U:T;
242
+
243
+
useAsyncEffect(function*() {
244
+
const result:ThenArg<ReturnType<
245
+
typeoffetchSomeData
246
+
>> =yieldfetchSomeData();
247
+
});
248
+
```
249
+
250
+
This is no ideal solution (and indeed prone to errors, due to typos or wrong type casting). However, it is still a bitter solution than go without types at all. In the future TypeScript might be able to improve the current situation.
0 commit comments