Skip to content

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

@danielrentz

Description

@danielrentz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions