[expect-type] Implement not.toBeCallableWith #142
Replies: 2 comments
-
I went down a similar path a couple of times, it's a tricky one. Responding to your proposals in reverse order: I think requiring the The Regarding the method that only works with one argument - something like that might actually be workable. Maybe a similar approach could be used for 2-, 3-, 4- and 5-arg functions, with any more than that not supported until someone requests them. I'd potentially be open to a PR along those lines, depending on how it looked in practice. In the meantime, since typescript 3.9 there is a decent way to ensure a function isn't callable with certain args: const f = (a: number) => a
// @ts-expect-error
expectTypeOf(f).toBeCallableWith('not a number') I will add an example along these lines to call this out explicitly in the docs. |
Beta Was this translation helpful? Give feedback.
-
@LorenzHenk see #152 - there might be something clever that can be done with "throw" types, assuming they land sometime soon (see microsoft/TypeScript#40468) - the use of the |
Beta Was this translation helpful? Give feedback.
-
At the moment,
not.toBeCallableWith
is not implemented (the function type isnever
).I read the source code and understand why you implemented the typing of
toBeCallableWith
in the way you did.I tried to implement it in several ways:
Type checking only the first argument
As typescript does not allow multiple spreads in 1 function parameter list, this approach allows you to check only the first argument.
Passing arguments as array
Did not work well. You have to use
as const
for your array. This makes the arrayreadonly
though, so you have to use a custom type to removereadonly
Using it
Most feature complete, but breaking change
You need to change the
obj: FunctionsDict
to not get a runtime error:Using it
The breaking change here is the additional need for
()
at the end. Sadly this can often be overlooked by the end user.Beta Was this translation helpful? Give feedback.
All reactions