Fix isFunction typing

This commit is contained in:
thepaperpilot 2022-09-26 18:41:10 -05:00
parent 73d6fd7566
commit 5bea1bf9b4
3 changed files with 17 additions and 14 deletions

View file

@ -10,7 +10,7 @@ import {
} from "features/feature";
import { globalBus } from "game/events";
import type { Persistent, State } from "game/persistence";
import { persistent, PersistentState } from "game/persistence";
import { persistent } from "game/persistence";
import type { Unsubscribe } from "nanoevents";
import { isFunction } from "util/common";
import type {
@ -336,7 +336,7 @@ export function createBoard<T extends BoardOptions>(
}
export function getNodeProperty<T>(property: NodeComputable<T>, node: BoardNode): T {
return isFunction(property) ? property(node) : unref(property);
return isFunction<T, [BoardNode], Computable<T>>(property) ? property(node) : unref(property);
}
export function getUniqueNodeID(board: GenericBoard): number {

View file

@ -12,9 +12,10 @@ export function camelToTitle(camel: string): string {
return title;
}
// eslint-disable-next-line @typescript-eslint/ban-types
export function isFunction(func: unknown): func is Function {
return typeof func === "function";
export function isFunction<T, S extends ReadonlyArray<unknown>, R>(
functionOrValue: ((...args: S) => T) | R
): functionOrValue is (...args: S) => T {
return typeof functionOrValue === "function";
}
export enum Direction {

View file

@ -13,13 +13,15 @@ describe("isFunction", () => {
test("Given function returns true", () => expect(isFunction(vi.fn())).toBe(true));
// Go through all primitives and basic types
test("Given a string returns false", () => expect(isFunction("test")).toBe(false));
test("Given a number returns false", () => expect(isFunction(10)).toBe(false));
test("Given a bigint returns false", () => expect(isFunction(BigInt(10))).toBe(false));
test("Given a boolean returns false", () => expect(isFunction(true)).toBe(false));
test("Given undefined returns false", () => expect(isFunction(undefined)).toBe(false));
test("Given a symbol returns false", () => expect(isFunction(Symbol())).toBe(false));
test("Given null returns false", () => expect(isFunction(null)).toBe(false));
test("Given object returns false", () => expect(isFunction({})).toBe(false));
test("Given array returns false", () => expect(isFunction([])).toBe(false));
test("Given a non-function returns false", () => {
expect(isFunction("test")).toBe(false);
expect(isFunction(10)).toBe(false);
expect(isFunction(BigInt(10))).toBe(false);
expect(isFunction(true)).toBe(false);
expect(isFunction(undefined)).toBe(false);
expect(isFunction(Symbol())).toBe(false);
expect(isFunction(null)).toBe(false);
expect(isFunction({})).toBe(false);
expect(isFunction([])).toBe(false);
});
});