Skip to content

Commit

Permalink
add an exposed MaybeAsyncIterable helper generic type
Browse files Browse the repository at this point in the history
  • Loading branch information
shtaif committed Dec 30, 2024
1 parent fe45bbb commit 060ea1a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/MaybeAsyncIterable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export { MaybeAsyncIterable };

/**
* Helper type that represents either a plain value or an async iterable of that value.
*
* This type is useful among else for typing props for components that can accept either a
* _"static" value_ or a _"changing" value_ (an async iterable) seamlessly.
*
* @example
* ```tsx
* import { It, type MaybeAsyncIterable } from 'react-async-iterators';
*
* function MyComponent(props: { values: MaybeAsyncIterable<string[]> }) {
* return (
* <ul>
* <It value={props.values} initialValue={[]}>
* {next => next.value.map((item, idx) =>
* <li key={idx}>{item}</li>
* )}
* </It>
* </ul>
* );
* }
* ```
*/
type MaybeAsyncIterable<T> = T | AsyncIterable<T>;

0 comments on commit 060ea1a

Please sign in to comment.