Skip to content

Commit

Permalink
fix: compiler error with function type narrowing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andnp committed Mar 13, 2019
1 parent 12ebbbb commit c797e95
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/none.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Maybe, { MatchType, Nil } from "./maybe";
import { maybe } from "./index";

const isNoArgFunc = <T>(x: any): x is (() => any) => {
return typeof x === "function" && x.length === 0;
};

const invokeFunc = <T>(funcOrT: T | (() => T)): T => {
if(typeof funcOrT === "function") {
if(isNoArgFunc(funcOrT)) {
return funcOrT();
}
return funcOrT;
Expand Down

0 comments on commit c797e95

Please sign in to comment.