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

support overloaded functions #30

Closed
y-polonsk opened this issue May 3, 2023 · 2 comments · Fixed by #83
Closed

support overloaded functions #30

y-polonsk opened this issue May 3, 2023 · 2 comments · Fixed by #83
Assignees
Labels
New feature New feature or request
Milestone

Comments

@y-polonsk
Copy link

Currently, the constructs like expectTypeOf.toBeCallableWith, expectTypeOf.parameters, expectTypeOf.parameter do not work with function overloads. Basically they just assume the signature of the last overload and give error otherwise.

I believe this could be made work, doing something similar to what is described here:

https://stackoverflow.com/questions/59535995/parameters-generic-of-overloaded-function-doesnt-contain-all-options/59538756#59538756

Although, for expectTypeOf.parameter, I m not sure how to refer to a parameter at given index for specific overload. But i believe "toBeCallableWith" and "parameters" could be made work.

Thanks.

@mmkal
Copy link
Owner

mmkal commented May 3, 2023

Oh that's an interesting workaround. Yes, I'd be open to adding that. Let me know if you would like to create a PR. There are some big-ish perf changes in flux right now, but this would probably be separate enough that it doesn't affect them.

mmkal added a commit that referenced this issue Oct 3, 2023
Test demonstrating the previous ugliness:
c3c4fd4
Fix: 89d81cc
Small improvement for regular old arrays:
76e6ece

Note: this will help if adding functionality for overloaded functions
#30
@aryaemami59
Copy link
Collaborator

@mmkal I'm gonna give this a shot!

@aryaemami59 aryaemami59 self-assigned this Mar 19, 2024
@aryaemami59 aryaemami59 added the New feature New feature or request label Mar 19, 2024
aryaemami59 added a commit that referenced this issue Aug 13, 2024
Related to #58
Related to #30

Improve support for overloaded functions (up to 10 overloads). So for an
example function type:

```ts
type Factorize = {
  (input: number): number[]
  (input: bigint): bigint[]
}
```

---

`.parameters` gives you an ExpectTypeOf instance with a union of the
parameter-tuple types, so:
 
```ts
expectTypeOf<Factorize>().parameters.toEqualTypeOf<[number] | [bigint]>()
```

---

`.returns` gives you an ExpectTypeOf instance with a union of the return
types, so

```ts
expectTypeOf<Factorize>().returns.toEqualTypeOf<number[] | bigint[]>()
```

---

`.toBeCallableWith(...)` now accepts any overload input, not just the
"last" like before. And you can now chain it via `.returns` which gives
you the matching return type:

```ts
expectTypeOf<Factorize>().toBeCallableWith(5).returns.toEqualTypeOf<number[]>()
```

---

## Implementation

- overload utilities were added
- initially, I thought all that was needed was a utility that matches
`F` typeargs against a single 10-overload type
- but the `test-types` job caught that this _doesn't_ work for
typescript <5.3
- for lower typescript versions, it seems we need an approach more like
the one in #58
- there are some edge cases the tests found for generic functions and
parameterless functions, so there is _sometimes_ some "useless" overload
info that has to be explicitly excluded
- there are a couple of intermediate utilities to check if the
10-overload type can be used, and to exclude "useless" overloads
- equivalents added for constructor parameters too
- used the overloaded versions in `DeepBrand` too

<details>
<summary>update: moved these changes to #93 to reduce diff</summary>

- I felt the megafile `index.ts` was finally getting too big with the
added overload utilties:
  - so those were put in a new `overloads.ts`
- since overload utils they rely on some other existing utils, I created
`utils.ts` to avoid circular references
  - `index.ts` remains as the home for the main `ExpectTypeOf` exports
- added `export * from './utils'` since we have been exporting all the
internal utils and I don't want to break people
  - update: moved these changes to #93 to reduce diff
</details>

---------

Co-authored-by: Misha Kaletsky <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Arya Emami <[email protected]>
@aryaemami59 aryaemami59 added this to the 1.0.0 milestone Aug 13, 2024
@aryaemami59 aryaemami59 linked a pull request Aug 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New feature New feature or request
Projects
None yet
3 participants