From b2f2caf6f45d75763fccde4b493531f267582005 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Thu, 18 Jun 2026 18:54:56 +0000 Subject: [PATCH] feat: add 'function' support to primitive type guards --- src/dyn.ts | 2 ++ src/fpSdk.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/dyn.ts b/src/dyn.ts index 3da5d0b..054d71a 100644 --- a/src/dyn.ts +++ b/src/dyn.ts @@ -25,6 +25,7 @@ export namespace Dyn { TCtor extends StringConstructor ? string : TCtor extends NumberConstructor ? number : TCtor extends BooleanConstructor ? boolean : + TCtor extends FunctionConstructor ? Function : InstanceType > { if (Check.if(variable).isNullish()) { @@ -61,6 +62,7 @@ export namespace Dyn { TCtor extends StringConstructor ? string : TCtor extends NumberConstructor ? number : TCtor extends BooleanConstructor ? boolean : + TCtor extends FunctionConstructor ? Function : InstanceType > { if (Check.if(variable).isNullish()) { diff --git a/src/fpSdk.test.ts b/src/fpSdk.test.ts index c174809..a237c9b 100644 --- a/src/fpSdk.test.ts +++ b/src/fpSdk.test.ts @@ -207,6 +207,47 @@ test("testing that Dyn.tryCast doesn't throw exceptions for 1st param, but does ); }); +test("testing Dyn.cast().to() with Function", () => { + const func1 = () => "hello"; + expect(Dyn.cast(func1).to(Function).isSome()).toBe(true); + + const func2 = function() { return "hello"; }; + expect(Dyn.cast(func2).to(Function).isSome()).toBe(true); + + const func3 = Function("return 'hello';"); + expect(Dyn.cast(func3).to(Function).isSome()).toBe(true); + + const nonFunc = "not a function"; + expect(Dyn.cast(nonFunc).to(Function).isSome()).toBe(false); + + const obj = {}; + expect(Dyn.cast(obj).to(Function).isSome()).toBe(false); + + const num = 42; + expect(Dyn.cast(num).to(Function).isSome()).toBe(false); +}); + +test("same as 'testing Dyn.cast().to() with Function' but with explicit types", () => { + const func1 = () => "hello"; + let castFunc1: Option = Dyn.cast(func1).to(Function); + expect(castFunc1.isSome()).toBe(true); + + const nonFunc = "not a function"; + let castNonFunc: Option = Dyn.cast(nonFunc).to(Function); + expect(castNonFunc.isSome()).toBe(false); +}); + +test("testing Dyn.tryCast().to() with Function", () => { + const funcNull = null; + expect(Dyn.tryCast(funcNull).to(Function).isSome()).toBe(false); + + const funcUndefined = undefined; + expect(Dyn.tryCast(funcUndefined).to(Function).isSome()).toBe(false); + + const func1 = () => "hello"; + expect(Dyn.tryCast(func1).to(Function).isSome()).toBe(true); +}); + function handleResult(result: Result): string { if (result instanceof Err) { return `Error: ${result.error}`;