From 5bea1bf9b4412ea4044a099e1be164cd126d56b5 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Mon, 26 Sep 2022 18:41:10 -0500 Subject: [PATCH] Fix isFunction typing --- src/features/boards/board.ts | 4 ++-- src/util/common.ts | 7 ++++--- tests/util/common.test.ts | 20 +++++++++++--------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/features/boards/board.ts b/src/features/boards/board.ts index 4c2cef5..4743370 100644 --- a/src/features/boards/board.ts +++ b/src/features/boards/board.ts @@ -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( } export function getNodeProperty(property: NodeComputable, node: BoardNode): T { - return isFunction(property) ? property(node) : unref(property); + return isFunction>(property) ? property(node) : unref(property); } export function getUniqueNodeID(board: GenericBoard): number { diff --git a/src/util/common.ts b/src/util/common.ts index b2ac8f1..d7560dd 100644 --- a/src/util/common.ts +++ b/src/util/common.ts @@ -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, R>( + functionOrValue: ((...args: S) => T) | R +): functionOrValue is (...args: S) => T { + return typeof functionOrValue === "function"; } export enum Direction { diff --git a/tests/util/common.test.ts b/tests/util/common.test.ts index abd1d00..619810f 100644 --- a/tests/util/common.test.ts +++ b/tests/util/common.test.ts @@ -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); + }); });