Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/dyn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export namespace Dyn {
TCtor extends StringConstructor ? string :
TCtor extends NumberConstructor ? number :
TCtor extends BooleanConstructor ? boolean :
TCtor extends FunctionConstructor ? Function :
InstanceType<TCtor>
> {
if (Check.if(variable).isNullish()) {
Expand Down Expand Up @@ -61,6 +62,7 @@ export namespace Dyn {
TCtor extends StringConstructor ? string :
TCtor extends NumberConstructor ? number :
TCtor extends BooleanConstructor ? boolean :
TCtor extends FunctionConstructor ? Function :
InstanceType<TCtor>
> {
if (Check.if(variable).isNullish()) {
Expand Down
41 changes: 41 additions & 0 deletions src/fpSdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Function> = Dyn.cast(func1).to(Function);
expect(castFunc1.isSome()).toBe(true);

const nonFunc = "not a function";
let castNonFunc: Option<Function> = 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<number, string>): string {
if (result instanceof Err) {
return `Error: ${result.error}`;
Expand Down
Loading