You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of writing, expectError() does not support checking if a value is uncallable:
constFoo=1;expectError(Foo());// ts(2349) This expression is not callable.// Type 'Number' has no call signatures.classMyClass{}expectError(MyClass());// ts(2348) Value of type 'typeof MyClass' is not callable.// Did you mean to include 'new'?
When I run tsd, it complains with "Expected an error, but found none." for both examples above.
Some questions:
Can expectError() be extended to add support for this? Does it need a new assertion function?
Is this within the scope of tsd? Or should I just use // @ts-expect-error instead?
Edit: I did some experiments.
classFoo{}constbar=1;functionbaz(): never{thrownewError();}// expectType<never>() is not good enough.// It cannot distinguish uncallable values from functions that never return at all// You also need @ts-expect-error anyway to stop TypeScript from complaining,// but this defeats the purpose of using tsd in the first place.// @ts-expect-errorexpectType<never>(Foo());// @ts-expect-errorexpectType<never>(bar());expectType<never>(baz());// expectNotAssignable<Function>() isn't good enough because it doesn't catch// attempting to call ES6 classes like plain functions.// But this seems to work!expectNotAssignable<CallableFunction>(Foo);expectNotAssignable<CallableFunction>(bar);expectAssignable<CallableFunction>(baz);
TL;DR: Use expectNotAssignable<CallableFunction>().
It might be nice to add this use case to the docs.
The text was updated successfully, but these errors were encountered:
pastelmind
changed the title
expectError() for uncallables?
How can I assert that a value is not a callable?
Apr 7, 2021
As of writing,
expectError()
does not support checking if a value is uncallable:When I run
tsd
, it complains with "Expected an error, but found none." for both examples above.Some questions:
CanexpectError()
be extended to add support for this? Does it need a new assertion function?Is this within the scope oftsd
? Or should I just use// @ts-expect-error
instead?Edit: I did some experiments.
TL;DR: Use
expectNotAssignable<CallableFunction>()
.It might be nice to add this use case to the docs.
The text was updated successfully, but these errors were encountered: