Skip to content

Commit

Permalink
Fixed initial value type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
smikhalevski committed Apr 30, 2024
1 parent 9a0e34e commit 0de0aab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/ExecutorManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbortablePromise, PubSub } from 'parallel-universe';
import { ExecutorImpl } from './ExecutorImpl';
import type { Executor, ExecutorEvent, ExecutorPlugin, ExecutorState, ExecutorTask } from './types';
import type { Executor, ExecutorEvent, ExecutorPlugin, ExecutorState, ExecutorTask, NoInfer } from './types';

/**
* Creates executors and manages their lifecycle.
Expand Down Expand Up @@ -67,7 +67,7 @@ export class ExecutorManager implements Iterable<Executor> {
getOrCreate<Value = any>(
key: string,
initialValue?: ExecutorTask<Value> | PromiseLike<Value> | Value,
plugins?: Array<ExecutorPlugin<Value> | undefined | null>
plugins?: Array<ExecutorPlugin<NoInfer<Value>> | undefined | null>
): Executor<Value>;

getOrCreate(key: string, initialValue?: unknown, plugins?: Array<ExecutorPlugin | undefined | null>): Executor {
Expand Down
7 changes: 7 additions & 0 deletions src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,10 @@ export interface Executor<Value = any> extends ExecutorState<Value> {
*/
toJSON(): ExecutorState<Value>;
}

/**
* Poor Man's NoInfer polyfill.
*/
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#type-inference-in-conditional-types
// https://devblogs.microsoft.com/typescript/announcing-typescript-5-4-beta/#the-noinfer-utility-type
export type NoInfer<T> = [T][T extends any ? 0 : never];
4 changes: 2 additions & 2 deletions src/main/useExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useDebugValue, useEffect, useState } from 'react';
import type { Executor, ExecutorPlugin, ExecutorTask } from './types';
import type { Executor, ExecutorPlugin, ExecutorTask, NoInfer } from './types';
import { useExecutorManager } from './useExecutorManager';

/**
Expand Down Expand Up @@ -39,7 +39,7 @@ export function useExecutor<Value = any>(
export function useExecutor<Value = any>(
key: string,
initialValue?: ExecutorTask<Value> | PromiseLike<Value> | Value,
plugins?: Array<ExecutorPlugin<Value> | undefined | null>
plugins?: Array<ExecutorPlugin<NoInfer<Value>> | undefined | null>
): Executor<Value>;

/**
Expand Down

0 comments on commit 0de0aab

Please sign in to comment.