Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

.map fn #98

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

.map fn #98

wants to merge 4 commits into from

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

commit: 20b5c29

pnpm add https://pkg.pr.new/mmkal/expect-type@98

Open in Stackblitz

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants