Skip to content

Commit c797e95

Browse files
committed
fix: compiler error with function type narrowing
The `invokeFunc` helper was throwing an error: >>> Cannot invoke an expression whose type lacks a call signature. Type '(() => T) | (T & Function)' has no compatible call signatures. Fixed by using a typeguard. Also increased runtime safety by ensuring that the function is 0-arg. If it requires more args, we won't be able to invoke it with this helper.
1 parent 12ebbbb commit c797e95

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/none.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import Maybe, { MatchType, Nil } from "./maybe";
22
import { maybe } from "./index";
33

4+
const isNoArgFunc = <T>(x: any): x is (() => any) => {
5+
return typeof x === "function" && x.length === 0;
6+
};
7+
48
const invokeFunc = <T>(funcOrT: T | (() => T)): T => {
5-
if(typeof funcOrT === "function") {
9+
if(isNoArgFunc(funcOrT)) {
610
return funcOrT();
711
}
812
return funcOrT;

0 commit comments

Comments
 (0)