CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

Improve TypeScript types for effect functions (hopeThat, tryTo, retryTo)

Open danielrentz opened this issue 4 months ago • 1 comments

The TypeScript types of the effect functions could be improved:

  1. hopeThat

    According to the source code, this function:

    • does not accept an asynchronous callback -- is this intentional?
    • does not use the result of the callback
    • returns a success flag

    Therefore, it can be made non-generic and with a synchronous callback.

    - type HopeThat = <T>(fn: () => Promise<T> | T) => Promise<T | false>;
    + type HopeThat = (fn: () => void) => Promise<boolean>;
    
  2. tryTo

    According to the source code, this function:

    • does not accept an asynchronous callback -- is this intentional?
    • does not use the result of the callback
    • returns a success flag

    Therefore, it can be made non-generic and with a synchronous callback.

    - type TryTo = <T>(fn: () => Promise<T> | T) => Promise<T | false>;
    + type TryTo = (fn: () => void) => Promise<boolean>;
    
  3. retryTo

    According to the source code, this function:

    • expects "maxTries" to be defined
    • accepts optional "pollIntervall"
    • passes iteration index to callback
    • does not use the result of the callback
    • returns a success flag

    Therefore, it can be made non-generic with an extended signature.

    - type RetryTo = <T>(fn: () => Promise<T> | T, retries?: number) => Promise<T>;
    + type RetryTo = (fn: (tries: number) => Promise<void> | void, maxTries: number, pollInterval?: number) => Promise<boolean>;
    

I would open a PR if intended support for asynchronous callbacks in hopeThat and tryTo is clear.

danielrentz avatar Sep 23 '25 07:09 danielrentz

PR #5335 fixes the type definitions according to the current implementation, i.e. tryTo and hopeThat cannot take an async callback function.

danielrentz avatar Nov 24 '25 13:11 danielrentz