Skip to content

Commit dd06927

Browse files
authored
feat: add an exposed MaybeAsyncIterable helper generic type (#25)
1 parent fe45bbb commit dd06927

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/MaybeAsyncIterable/index.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export { MaybeAsyncIterable };
2+
3+
/**
4+
* Helper type that represents either a plain value or an async iterable of that value.
5+
*
6+
* This type is useful among else for typing props for components that can accept either a
7+
* _"static" value_ or a _"changing" value_ (an async iterable) seamlessly.
8+
*
9+
* @example
10+
* ```tsx
11+
* import { It, type MaybeAsyncIterable } from 'react-async-iterators';
12+
*
13+
* function MyComponent(props: { values: MaybeAsyncIterable<string[]> }) {
14+
* return (
15+
* <ul>
16+
* <It value={props.values} initialValue={[]}>
17+
* {next => next.value.map((item, idx) =>
18+
* <li key={idx}>{item}</li>
19+
* )}
20+
* </It>
21+
* </ul>
22+
* );
23+
* }
24+
* ```
25+
*/
26+
type MaybeAsyncIterable<T> = T | AsyncIterable<T>;

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useAsyncIter, type IterationResult } from './useAsyncIter/index.js';
22
import { Iterate, type IterateProps } from './Iterate/index.js';
33
import { iterateFormatted, type FixedRefFormattedIterable } from './iterateFormatted/index.js';
44
import { useAsyncIterState, type AsyncIterStateResult } from './useAsyncIterState/index.js';
5+
import { type MaybeAsyncIterable } from './MaybeAsyncIterable/index.js';
56

67
export {
78
useAsyncIter,
@@ -13,4 +14,5 @@ export {
1314
type FixedRefFormattedIterable,
1415
useAsyncIterState,
1516
type AsyncIterStateResult,
17+
type MaybeAsyncIterable,
1618
};

0 commit comments

Comments
 (0)