Skip to content
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

fix(types)!: do not try to infer types of overloaded functions #2

Merged
merged 4 commits into from
Oct 7, 2023

Conversation

mcous
Copy link
Owner

@mcous mcous commented Sep 23, 2023

I had originally tried to support parameter and return type inference for overloaded functions in this library. TypeScript does not support this inference natively, so I used a (very cool) recursive type annotation to extract the different overload types.

This recursive utility, however, is prone to infinite recursion in various cases that I feel are more common that overloaded functions. In order to preserve a better experience for the general use case, this PR removes custom parameter and return type inference for overloaded functions.

This makes inference for non-overloaded functions much simpler and more reliable. For overloaded functions, there are now two options:

  • TypeScript's native Parameter and ReturnType utilities will always infer the last overload; so if you're stubbing based on the last overload, you're good
  • For other overloads, you can use the TFunc type parameter of when
    • e.g. when<(a: string) => string>(overloadedFn).calledWith('hello').thenReturn('world')

@mcous mcous force-pushed the fix-generic-type-recursion branch from 1456a1e to f59ed24 Compare October 7, 2023 17:29
@mcous mcous changed the title fix(types): prevent infinite type recursion with generic functions fix(types)!: do not try to infer types of overloaded functions Oct 7, 2023
@mcous mcous merged commit d1b17ef into main Oct 7, 2023
12 checks passed
@mcous mcous deleted the fix-generic-type-recursion branch October 7, 2023 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant