Skip to content

Fix <Iterate> render function parameter typing not inferring correctly #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions spec/tests/Iterate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('`Iterate` component', () => {
it(gray('When given an iterable that yields a value will render correctly'), async () => {
const channel = new IterableChannelTestHelper<string>();
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const rendered = render(
<Iterate value={channel}>
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('`Iterate` component', () => {
it(gray('When given an iterable that yields multiple values will render correctly'), async () => {
const channel = new IterableChannelTestHelper<string>();
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const rendered = render(
<Iterate value={channel}>
Expand Down Expand Up @@ -267,7 +267,7 @@ describe('`Iterate` component', () => {
async () => {
const emptyIter = (async function* () {})();
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const rendered = render(
<Iterate value={emptyIter}>
Expand Down Expand Up @@ -333,7 +333,7 @@ describe('`Iterate` component', () => {
async () => {
const channel = new IterableChannelTestHelper<string>();
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const rendered = render(
<Iterate value={channel}>
Expand Down Expand Up @@ -380,7 +380,7 @@ describe('`Iterate` component', () => {
throw simulatedError;
})();
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const rendered = render(
<Iterate value={erroringIter}>
Expand Down Expand Up @@ -448,7 +448,7 @@ describe('`Iterate` component', () => {
async () => {
const channel = new IterableChannelTestHelper<string>();
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const simulatedErr = new Error('...');

Expand Down Expand Up @@ -495,7 +495,7 @@ describe('`Iterate` component', () => {
"When consecutively updated with new iterables will close the previous one's iterator every time and render accordingly"
),
async () => {
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const [channel1, channel2] = [
new IterableChannelTestHelper<string>(),
Expand Down Expand Up @@ -585,7 +585,7 @@ describe('`Iterate` component', () => {
);

it(gray('When unmounted will close the last active iterator it held'), async () => {
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const channel = new IterableChannelTestHelper<string>();
const channelReturnSpy = vi.spyOn(channel, 'return');
Expand Down Expand Up @@ -641,7 +641,7 @@ describe('`Iterate` component', () => {
yield* ['a', 'b', 'c'];
})();
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const rendered = render(
<Iterate value={iter}>
Expand Down Expand Up @@ -674,7 +674,7 @@ describe('`Iterate` component', () => {
),
async () => {
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;
let lastRenderFnInput: undefined | IterationResult<string | undefined>;
const channel = new IterableChannelTestHelper<string>();

const rendered = render(
Expand Down
5 changes: 4 additions & 1 deletion src/Iterate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ function Iterate<TVal, TInitialVal = undefined>(props: IterateProps<TVal, TIniti
typeof props.children === 'function'
? (() => {
const propsBetterTyped = props as IteratePropsWithRenderFunction<TVal, TInitialVal>;
const next = useAsyncIter(propsBetterTyped.value, propsBetterTyped.initialValue);
const next = useAsyncIter(
propsBetterTyped.value,
propsBetterTyped.initialValue as TInitialVal
);
return propsBetterTyped.children(next);
})()
: (() => {
Expand Down
14 changes: 7 additions & 7 deletions src/useAsyncIter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export { useAsyncIter, type IterationResult };
* ```
*/
const useAsyncIter: {
<TVal>(input: TVal, initialVal?: undefined): IterationResult<TVal, undefined>;
<TVal, TInitVal = undefined>(input: TVal, initialVal?: TInitVal): IterationResult<TVal, TInitVal>;
<TVal>(input: TVal, initialVal?: undefined): IterationResult<TVal>;
<TVal, TInitVal>(input: TVal, initialVal: TInitVal): IterationResult<TVal, TInitVal>;
} = <
TVal extends
| undefined
Expand All @@ -120,7 +120,7 @@ const useAsyncIter: {
const rerender = useSimpleRerender();

const stateRef = useRef<IterationResult<TVal, TInitVal>>({
value: initialVal,
value: initialVal as any,
pendingFirst: true,
done: false,
error: undefined,
Expand Down Expand Up @@ -225,12 +225,12 @@ const useAsyncIter: {
type IterationResult<TVal, TInitVal = undefined> = {
/**
* The most recent value received from the async iterable iteration, starting as {@link TInitVal}.
* If the source was instead a plain value, it will simply be it.
* If the source was a plain value instead, it will simply be it, ignoring any {@link TInitVal}.
*
* Starting to iterate a new async iterable at any future point on itself doesn't reset this;
* only some newly resolved next value will.
* When the source iterable changes and an iteration restarts with a new iterable, the same last
* `value` is carried over and reflected until the new iterable resolves its first value.
* */
value: ExtractAsyncIterValue<TVal> | TInitVal;
value: TVal extends AsyncIterable<infer J> ? J | TInitVal : TVal;

/**
* Indicates whether the iterated async iterable is still pending its own first value to be
Expand Down
Loading