Skip to content

Conversation

mmkal
Copy link
Owner

@mmkal mmkal commented Aug 12, 2024

@aryaemami59 another potential helper that occurred to me might be useful. I'm not totally convinced it's worth it, but thought I'd open a draft PR in case you had thoughts. Given .toBeCallableWith doesn't work well for generic functions, it's a catch-all way to say "I am allowed to do this with my object" and make assertions on the result, without actually calling the function or performing side-effects, etc.:

const capitalize = <S extends string>(input: S) =>
  (input.slice(0, 1).toUpperCase() + input.slice(1)) as Capitalize<S>

expectTypeOf(capitalize)
  .map(fn => fn('hello world'))
  .toEqualTypeOf<'Hello world'>()

But the assertion could just as easily (maybe more easily) be written as:

expectTypeOf(() => capitalize('hello world')).returns.toEqualTypeOf<'Hello world'>()

Which also doesn't actually call capitalize at runtime.

Advantages of .map:

  • it makes it clearer it's an asertion about capitalize rather than an inline callback.
  • it works fine with expectTypeOf<X>() as well as expectTypeOf(x)
  • implementation is v simple so the cost is low

It could also sub in for how we do .toBeCallableWith in #83 too:

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

expectTypeOf<Factorize>().map(f => f(12)).toEqualTypeOf<number[]>()
expectTypeOf<Factorize>().map(f => f(12n)).toEqualTypeOf<bigint[]>()

@aryaemami59
Copy link
Collaborator

Not sure about this one yet, I'd say let's get #83 in first then we can use this one to potentially fill in any left over gaps.

Copy link

pkg-pr-new bot commented Aug 13, 2024

Open in Stackblitz

npm i https://pkg.pr.new/mmkal/expect-type@98

commit: 1850004

@mmkal mmkal marked this pull request as ready for review February 13, 2025 10:54
@mmkal
Copy link
Owner Author

mmkal commented Feb 13, 2025

@aryaemami59 I keep forgetting this is still a draft. Not a particularly exciting feature but v small implementation cost and somewhat useful so I'd like to get it in. Let me know if you have any thoughts on the code.

Co-authored-by: Arya Emami <aryaemami59@yahoo.com>
@mmkal mmkal requested a review from aryaemami59 February 20, 2025 15:31
Copy link
Collaborator

@aryaemami59 aryaemami59 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not too sure about the name tbh, and we'll add more docs and examples later to cover some of the niche use cases. Other than all looks good to me.

@aryaemami59 aryaemami59 added this to the 2.0.0 milestone Feb 28, 2025
@aryaemami59 aryaemami59 added the New feature New feature or request label Feb 28, 2025
@mmkal mmkal removed this from the 2.0.0 milestone Feb 28, 2025
@mmkal mmkal merged commit 97f72a5 into main Feb 28, 2025
29 checks passed
@mmkal mmkal deleted the map branch February 28, 2025 17:43
@aryaemami59 aryaemami59 removed the New feature New feature or request label Feb 28, 2025
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.

2 participants