From 17e0293afe7078427700f1a00695ab3a6c29faa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20G=2E=20Aragoneses?= Date: Mon, 25 May 2026 15:47:10 +0800 Subject: [PATCH 1/5] BREAKING: rename isInstanceOf(x,y) to cast(x).to(y) It is way more readable, because this way you don't have to check which param is the type. --- src/fpSdk.test.ts | 59 +++++++++++++++------------------------------- src/typeHelpers.ts | 40 +++++++++++++++++-------------- 2 files changed, 41 insertions(+), 58 deletions(-) diff --git a/src/fpSdk.test.ts b/src/fpSdk.test.ts index 4c57b08..c9768dc 100644 --- a/src/fpSdk.test.ts +++ b/src/fpSdk.test.ts @@ -69,74 +69,53 @@ class Bar { } } -test("testing TypeHelpers.IsInstanceOf", () => { +test("testing TypeHelpers.cast.to", () => { const str1 = "foo"; - expect(TypeHelpers.isInstanceOf(str1, String)).toBe(true); + expect(TypeHelpers.cast(str1).to(String)).toBe(true); const str2 = String("foo"); - expect(TypeHelpers.isInstanceOf(str2, String)).toBe(true); + expect(TypeHelpers.cast(str2).to(String)).toBe(true); //commented this one because prettier complains about it, but it works: //let str3 = 'foo'; - //expect(TypeHelpers.isInstanceOf(str3, String)).toBe(true); + //expect(TypeHelpers.cast(str3).to(String)).toBe(true); const nonStr = 3; - expect(TypeHelpers.isInstanceOf(nonStr, String)).toBe(false); + expect(TypeHelpers.cast(nonStr).to(String)).toBe(false); const int1 = 2; - expect(TypeHelpers.isInstanceOf(int1, Number)).toBe(true); + expect(TypeHelpers.cast(int1).to(Number)).toBe(true); const int2 = Number(2); - expect(TypeHelpers.isInstanceOf(int2, Number)).toBe(true); + expect(TypeHelpers.cast(int2).to(Number)).toBe(true); const nonInt = "2"; - expect(TypeHelpers.isInstanceOf(nonInt, Number)).toBe(false); + expect(TypeHelpers.cast(nonInt).to(Number)).toBe(false); const foo = new Foo(); const bar = new Bar(); - expect(TypeHelpers.isInstanceOf(foo, Foo)).toBe(true); - expect(TypeHelpers.isInstanceOf(bar, Bar)).toBe(true); - expect(TypeHelpers.isInstanceOf(foo, Bar)).toBe(false); - expect(TypeHelpers.isInstanceOf(bar, Foo)).toBe(false); + expect(TypeHelpers.cast(foo).to(Foo)).toBe(true); + expect(TypeHelpers.cast(bar).to(Bar)).toBe(true); + expect(TypeHelpers.cast(foo).to(Bar)).toBe(false); + expect(TypeHelpers.cast(bar).to(Foo)).toBe(false); }); -test("testing TypeHelpers.isInstanceOf exceptions", () => { +test("testing TypeHelpers.cast.to exceptions", () => { const strNull = null; - expect(() => TypeHelpers.isInstanceOf(strNull, String)).toThrowError( + expect(() => TypeHelpers.cast(strNull).to(String)).toThrowError( "Invalid" ); - expect(() => TypeHelpers.isInstanceOf(strNull, String)).toThrowError( + expect(() => TypeHelpers.cast(strNull).to(String)).toThrowError( "parameter" ); - expect(() => TypeHelpers.isInstanceOf(strNull, String)).toThrowError( + expect(() => TypeHelpers.cast(strNull).to(String)).toThrowError( "null" ); const strUndefined = undefined; - expect(() => TypeHelpers.isInstanceOf(strUndefined, String)).toThrowError( + expect(() => TypeHelpers.cast(strUndefined).to(String)).toThrowError( "Invalid" ); - expect(() => TypeHelpers.isInstanceOf(strUndefined, String)).toThrowError( + expect(() => TypeHelpers.cast(strUndefined).to(String)).toThrowError( "parameter" ); - expect(() => TypeHelpers.isInstanceOf(strUndefined, String)).toThrowError( - "undefined" - ); - - const typeNull = null; - expect(() => TypeHelpers.isInstanceOf("foo", typeNull)).toThrowError( - "Invalid" - ); - expect(() => TypeHelpers.isInstanceOf("foo", typeNull)).toThrowError( - "parameter" - ); - expect(() => TypeHelpers.isInstanceOf("foo", typeNull)).toThrowError( - "null" - ); - const typeUndefined = undefined; - expect(() => TypeHelpers.isInstanceOf("foo", typeUndefined)).toThrowError( - "Invalid" - ); - expect(() => TypeHelpers.isInstanceOf("foo", typeUndefined)).toThrowError( - "parameter" - ); - expect(() => TypeHelpers.isInstanceOf("foo", typeUndefined)).toThrowError( + expect(() => TypeHelpers.cast(strUndefined).to(String)).toThrowError( "undefined" ); }); diff --git a/src/typeHelpers.ts b/src/typeHelpers.ts index 9c12e5e..bb384aa 100644 --- a/src/typeHelpers.ts +++ b/src/typeHelpers.ts @@ -6,24 +6,28 @@ export class TypeHelpers { // because instanceof doesn't work with primitive types (e.g. String), taken from https://stackoverflow.com/a/58184883/544947 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- method accepts any value by design - public static isInstanceOf(variable: any, type: any) { - if (TypeHelpers.isNullOrUndefined(variable)) { - throw new Error( - "Invalid 'variable' parameter passed in: null or undefined" - ); - } - if (TypeHelpers.isNullOrUndefined(type)) { - throw new Error( - "Invalid 'type' parameter passed in: null or undefined" - ); - } + public static cast(variable: any) { + return { + to(type: any): boolean { + if (TypeHelpers.isNullOrUndefined(variable)) { + throw new Error( + "Invalid 'variable' parameter passed in: null or undefined" + ); + } + if (TypeHelpers.isNullOrUndefined(type)) { + throw new Error( + "Invalid 'type' parameter passed in: null or undefined" + ); + } - let res: boolean = false; - if (typeof type == "string") { - res = typeof variable == type.toLowerCase(); - } else { - res = variable.constructor == type; - } - return res; + let res: boolean = false; + if (typeof type == "string") { + res = typeof variable == type.toLowerCase(); + } else { + res = variable.constructor == type; + } + return res; + }, + }; } } From 34d4831c88eb8752fa0ca93777263da33a8c427e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20G=2E=20Aragoneses?= Date: Mon, 25 May 2026 16:01:58 +0800 Subject: [PATCH 2/5] Added some String API that I miss from dotnet --- src/fpSdk.test.ts | 20 ++++++++++++++++++++ src/typeHelpers.ts | 17 +++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/fpSdk.test.ts b/src/fpSdk.test.ts index c9768dc..3213a4e 100644 --- a/src/fpSdk.test.ts +++ b/src/fpSdk.test.ts @@ -97,6 +97,26 @@ test("testing TypeHelpers.cast.to", () => { expect(TypeHelpers.cast(bar).to(Foo)).toBe(false); }); +test("testing TypeHelpers.stringIsNullishOrEmpty", () => { + expect(TypeHelpers.stringIsNullishOrEmpty(null as any)).toBe(true); + expect(TypeHelpers.stringIsNullishOrEmpty(undefined as any)).toBe(true); + expect(TypeHelpers.stringIsNullishOrEmpty("")).toBe(true); + expect(TypeHelpers.stringIsNullishOrEmpty("hello")).toBe(false); + expect(TypeHelpers.stringIsNullishOrEmpty(" ")).toBe(false); +}); + +test("testing TypeHelpers.stringIsNullishOrWhiteSpace", () => { + expect(TypeHelpers.stringIsNullishOrWhiteSpace(null as any)).toBe(true); + expect(TypeHelpers.stringIsNullishOrWhiteSpace(undefined as any)).toBe(true); + expect(TypeHelpers.stringIsNullishOrWhiteSpace("")).toBe(true); + expect(TypeHelpers.stringIsNullishOrWhiteSpace(" ")).toBe(true); + expect(TypeHelpers.stringIsNullishOrWhiteSpace("\t")).toBe(true); + expect(TypeHelpers.stringIsNullishOrWhiteSpace("\n")).toBe(true); + expect(TypeHelpers.stringIsNullishOrWhiteSpace(" \t\n ")).toBe(true); + expect(TypeHelpers.stringIsNullishOrWhiteSpace("hello")).toBe(false); + expect(TypeHelpers.stringIsNullishOrWhiteSpace(" hello ")).toBe(false); +}); + test("testing TypeHelpers.cast.to exceptions", () => { const strNull = null; expect(() => TypeHelpers.cast(strNull).to(String)).toThrowError( diff --git a/src/typeHelpers.ts b/src/typeHelpers.ts index bb384aa..29fe8e1 100644 --- a/src/typeHelpers.ts +++ b/src/typeHelpers.ts @@ -4,6 +4,23 @@ export class TypeHelpers { return variable === null || variable === undefined; } + public static stringIsNullishOrEmpty(str: string) { + if (TypeHelpers.isNullOrUndefined(str)) { + return true; + } + if (str.length === 0) { + return true; + } + return false; + } + + public static stringIsNullishOrWhiteSpace(str: string) { + if (TypeHelpers.stringIsNullishOrEmpty(str)) { + return true; + } + return str.trim().length === 0; + } + // because instanceof doesn't work with primitive types (e.g. String), taken from https://stackoverflow.com/a/58184883/544947 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- method accepts any value by design public static cast(variable: any) { From 04fa4b176ca4af48b54e9a6e735784b3cbe468b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20G=2E=20Aragoneses?= Date: Mon, 25 May 2026 16:31:39 +0800 Subject: [PATCH 3/5] Change casting to return T instead of bool Most of the time when you're checking if a var is of type T, you want to cast afterwards if the result is true, so let's reuse Option types here. If you don't want to cast, you simply use isSome() or isNone() later. --- src/fpSdk.test.ts | 34 +++++++++++++++++----------------- src/typeHelpers.ts | 18 ++++-------------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/fpSdk.test.ts b/src/fpSdk.test.ts index 3213a4e..ffec098 100644 --- a/src/fpSdk.test.ts +++ b/src/fpSdk.test.ts @@ -71,30 +71,30 @@ class Bar { test("testing TypeHelpers.cast.to", () => { const str1 = "foo"; - expect(TypeHelpers.cast(str1).to(String)).toBe(true); + expect(TypeHelpers.cast(str1).to().isSome()).toBe(true); const str2 = String("foo"); - expect(TypeHelpers.cast(str2).to(String)).toBe(true); + expect(TypeHelpers.cast(str2).to().isSome()).toBe(true); //commented this one because prettier complains about it, but it works: //let str3 = 'foo'; - //expect(TypeHelpers.cast(str3).to(String)).toBe(true); + //expect(TypeHelpers.cast(str3).to().isSome()).toBe(true); const nonStr = 3; - expect(TypeHelpers.cast(nonStr).to(String)).toBe(false); + expect(TypeHelpers.cast(nonStr).to().isSome()).toBe(true); const int1 = 2; - expect(TypeHelpers.cast(int1).to(Number)).toBe(true); + expect(TypeHelpers.cast(int1).to().isSome()).toBe(true); const int2 = Number(2); - expect(TypeHelpers.cast(int2).to(Number)).toBe(true); + expect(TypeHelpers.cast(int2).to().isSome()).toBe(true); const nonInt = "2"; - expect(TypeHelpers.cast(nonInt).to(Number)).toBe(false); + expect(TypeHelpers.cast(nonInt).to().isSome()).toBe(true); const foo = new Foo(); const bar = new Bar(); - expect(TypeHelpers.cast(foo).to(Foo)).toBe(true); - expect(TypeHelpers.cast(bar).to(Bar)).toBe(true); - expect(TypeHelpers.cast(foo).to(Bar)).toBe(false); - expect(TypeHelpers.cast(bar).to(Foo)).toBe(false); + expect(TypeHelpers.cast(foo).to().isSome()).toBe(true); + expect(TypeHelpers.cast(bar).to().isSome()).toBe(true); + expect(TypeHelpers.cast(foo).to().isSome()).toBe(true); + expect(TypeHelpers.cast(bar).to().isSome()).toBe(true); }); test("testing TypeHelpers.stringIsNullishOrEmpty", () => { @@ -119,23 +119,23 @@ test("testing TypeHelpers.stringIsNullishOrWhiteSpace", () => { test("testing TypeHelpers.cast.to exceptions", () => { const strNull = null; - expect(() => TypeHelpers.cast(strNull).to(String)).toThrowError( + expect(() => TypeHelpers.cast(strNull).to()).toThrowError( "Invalid" ); - expect(() => TypeHelpers.cast(strNull).to(String)).toThrowError( + expect(() => TypeHelpers.cast(strNull).to()).toThrowError( "parameter" ); - expect(() => TypeHelpers.cast(strNull).to(String)).toThrowError( + expect(() => TypeHelpers.cast(strNull).to()).toThrowError( "null" ); const strUndefined = undefined; - expect(() => TypeHelpers.cast(strUndefined).to(String)).toThrowError( + expect(() => TypeHelpers.cast(strUndefined).to()).toThrowError( "Invalid" ); - expect(() => TypeHelpers.cast(strUndefined).to(String)).toThrowError( + expect(() => TypeHelpers.cast(strUndefined).to()).toThrowError( "parameter" ); - expect(() => TypeHelpers.cast(strUndefined).to(String)).toThrowError( + expect(() => TypeHelpers.cast(strUndefined).to()).toThrowError( "undefined" ); }); diff --git a/src/typeHelpers.ts b/src/typeHelpers.ts index 29fe8e1..a58b7c4 100644 --- a/src/typeHelpers.ts +++ b/src/typeHelpers.ts @@ -1,3 +1,5 @@ +import { Option, Some } from "./option.js"; + export class TypeHelpers { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- method accepts any value by design public static isNullOrUndefined(variable: any) { @@ -25,25 +27,13 @@ export class TypeHelpers { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- method accepts any value by design public static cast(variable: any) { return { - to(type: any): boolean { + to(): Option { if (TypeHelpers.isNullOrUndefined(variable)) { throw new Error( "Invalid 'variable' parameter passed in: null or undefined" ); } - if (TypeHelpers.isNullOrUndefined(type)) { - throw new Error( - "Invalid 'type' parameter passed in: null or undefined" - ); - } - - let res: boolean = false; - if (typeof type == "string") { - res = typeof variable == type.toLowerCase(); - } else { - res = variable.constructor == type; - } - return res; + return new Some(variable); }, }; } From 6ab40df6ffd4dc48b1322a06f6b7528d0d5ecee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20G=2E=20Aragoneses?= Date: Mon, 25 May 2026 16:47:47 +0800 Subject: [PATCH 4/5] Move casting to Dyn type Sounds better and shorter. --- src/dyn.ts | 18 ++++++++++++++++++ src/fpSdk.test.ts | 39 ++++++++++++++++++++------------------- src/index.ts | 1 + src/typeHelpers.ts | 17 ----------------- 4 files changed, 39 insertions(+), 36 deletions(-) create mode 100644 src/dyn.ts diff --git a/src/dyn.ts b/src/dyn.ts new file mode 100644 index 0000000..79ce31c --- /dev/null +++ b/src/dyn.ts @@ -0,0 +1,18 @@ +import { Option, Some } from "./option.js"; +import { TypeHelpers } from "./typeHelpers.js"; + +export class Dyn { + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- method accepts any value by design + public static cast(variable: any) { + return { + to(): Option { + if (TypeHelpers.isNullOrUndefined(variable)) { + throw new Error( + "Invalid 'variable' parameter passed in: null or undefined" + ); + } + return new Some(variable); + }, + }; + } +} diff --git a/src/fpSdk.test.ts b/src/fpSdk.test.ts index ffec098..1b19cf7 100644 --- a/src/fpSdk.test.ts +++ b/src/fpSdk.test.ts @@ -9,6 +9,7 @@ import { Err, Result, TypeHelpers, + Dyn, } from "./index.js"; function typeGuard(option: Option) { @@ -69,32 +70,32 @@ class Bar { } } -test("testing TypeHelpers.cast.to", () => { +test("testing Dyn.cast.to", () => { const str1 = "foo"; - expect(TypeHelpers.cast(str1).to().isSome()).toBe(true); + expect(Dyn.cast(str1).to().isSome()).toBe(true); const str2 = String("foo"); - expect(TypeHelpers.cast(str2).to().isSome()).toBe(true); + expect(Dyn.cast(str2).to().isSome()).toBe(true); //commented this one because prettier complains about it, but it works: //let str3 = 'foo'; - //expect(TypeHelpers.cast(str3).to().isSome()).toBe(true); + //expect(Dyn.cast(str3).to().isSome()).toBe(true); const nonStr = 3; - expect(TypeHelpers.cast(nonStr).to().isSome()).toBe(true); + expect(Dyn.cast(nonStr).to().isSome()).toBe(true); const int1 = 2; - expect(TypeHelpers.cast(int1).to().isSome()).toBe(true); + expect(Dyn.cast(int1).to().isSome()).toBe(true); const int2 = Number(2); - expect(TypeHelpers.cast(int2).to().isSome()).toBe(true); + expect(Dyn.cast(int2).to().isSome()).toBe(true); const nonInt = "2"; - expect(TypeHelpers.cast(nonInt).to().isSome()).toBe(true); + expect(Dyn.cast(nonInt).to().isSome()).toBe(true); const foo = new Foo(); const bar = new Bar(); - expect(TypeHelpers.cast(foo).to().isSome()).toBe(true); - expect(TypeHelpers.cast(bar).to().isSome()).toBe(true); - expect(TypeHelpers.cast(foo).to().isSome()).toBe(true); - expect(TypeHelpers.cast(bar).to().isSome()).toBe(true); + expect(Dyn.cast(foo).to().isSome()).toBe(true); + expect(Dyn.cast(bar).to().isSome()).toBe(true); + expect(Dyn.cast(foo).to().isSome()).toBe(true); + expect(Dyn.cast(bar).to().isSome()).toBe(true); }); test("testing TypeHelpers.stringIsNullishOrEmpty", () => { @@ -117,25 +118,25 @@ test("testing TypeHelpers.stringIsNullishOrWhiteSpace", () => { expect(TypeHelpers.stringIsNullishOrWhiteSpace(" hello ")).toBe(false); }); -test("testing TypeHelpers.cast.to exceptions", () => { +test("testing Dyn.cast.to exceptions", () => { const strNull = null; - expect(() => TypeHelpers.cast(strNull).to()).toThrowError( + expect(() => Dyn.cast(strNull).to()).toThrowError( "Invalid" ); - expect(() => TypeHelpers.cast(strNull).to()).toThrowError( + expect(() => Dyn.cast(strNull).to()).toThrowError( "parameter" ); - expect(() => TypeHelpers.cast(strNull).to()).toThrowError( + expect(() => Dyn.cast(strNull).to()).toThrowError( "null" ); const strUndefined = undefined; - expect(() => TypeHelpers.cast(strUndefined).to()).toThrowError( + expect(() => Dyn.cast(strUndefined).to()).toThrowError( "Invalid" ); - expect(() => TypeHelpers.cast(strUndefined).to()).toThrowError( + expect(() => Dyn.cast(strUndefined).to()).toThrowError( "parameter" ); - expect(() => TypeHelpers.cast(strUndefined).to()).toThrowError( + expect(() => Dyn.cast(strUndefined).to()).toThrowError( "undefined" ); }); diff --git a/src/index.ts b/src/index.ts index b79c635..468892b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,5 +8,6 @@ export type { Result } from './result.js'; // Export helpers export { TypeHelpers } from './typeHelpers.js'; +export { Dyn } from './dyn.js'; export { Empty } from './empty.js'; diff --git a/src/typeHelpers.ts b/src/typeHelpers.ts index a58b7c4..44677be 100644 --- a/src/typeHelpers.ts +++ b/src/typeHelpers.ts @@ -1,5 +1,3 @@ -import { Option, Some } from "./option.js"; - export class TypeHelpers { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- method accepts any value by design public static isNullOrUndefined(variable: any) { @@ -22,19 +20,4 @@ export class TypeHelpers { } return str.trim().length === 0; } - - // because instanceof doesn't work with primitive types (e.g. String), taken from https://stackoverflow.com/a/58184883/544947 - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- method accepts any value by design - public static cast(variable: any) { - return { - to(): Option { - if (TypeHelpers.isNullOrUndefined(variable)) { - throw new Error( - "Invalid 'variable' parameter passed in: null or undefined" - ); - } - return new Some(variable); - }, - }; - } } From bbab8691448e6ffc4b90a5d78d3504e25e45ab6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20G=2E=20Aragoneses?= Date: Mon, 25 May 2026 17:06:47 +0800 Subject: [PATCH 5/5] WIP: fix regression in tests?? One of the last commits left this shit: ``` expect(Dyn.cast(foo).to().isSome()).toBe(true); expect(Dyn.cast(bar).to().isSome()).toBe(true); expect(Dyn.cast(foo).to().isSome()).toBe(true); expect(Dyn.cast(bar).to().isSome()).toBe(true); ``` --- src/dyn.ts | 21 ++++++++++++++--- src/fpSdk.test.ts | 57 +++++++++++++++++++++-------------------------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/dyn.ts b/src/dyn.ts index 79ce31c..b10bf4a 100644 --- a/src/dyn.ts +++ b/src/dyn.ts @@ -1,17 +1,32 @@ -import { Option, Some } from "./option.js"; +import { None, Option, Some } from "./option.js"; import { TypeHelpers } from "./typeHelpers.js"; export class Dyn { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- method accepts any value by design public static cast(variable: any) { return { - to(): Option { + to(type: unknown): Option { if (TypeHelpers.isNullOrUndefined(variable)) { throw new Error( "Invalid 'variable' parameter passed in: null or undefined" ); } - return new Some(variable); + if (TypeHelpers.isNullOrUndefined(type)) { + throw new Error( + "Invalid 'type' parameter passed in: null or undefined" + ); + } + + let res: boolean = false; + if (typeof type === "string") { + res = typeof variable === type.toLowerCase(); + } else if (typeof type === "function") { + res = variable.constructor === type; + } + if (res) { + return new Some(variable); + } + return new None(); }, }; } diff --git a/src/fpSdk.test.ts b/src/fpSdk.test.ts index 1b19cf7..31c317b 100644 --- a/src/fpSdk.test.ts +++ b/src/fpSdk.test.ts @@ -72,30 +72,26 @@ class Bar { test("testing Dyn.cast.to", () => { const str1 = "foo"; - expect(Dyn.cast(str1).to().isSome()).toBe(true); + expect(Dyn.cast(str1).to(String).isSome()).toBe(true); const str2 = String("foo"); - expect(Dyn.cast(str2).to().isSome()).toBe(true); - - //commented this one because prettier complains about it, but it works: - //let str3 = 'foo'; - //expect(Dyn.cast(str3).to().isSome()).toBe(true); + expect(Dyn.cast(str2).to(String).isSome()).toBe(true); const nonStr = 3; - expect(Dyn.cast(nonStr).to().isSome()).toBe(true); + expect(Dyn.cast(nonStr).to(String).isSome()).toBe(false); const int1 = 2; - expect(Dyn.cast(int1).to().isSome()).toBe(true); + expect(Dyn.cast(int1).to(Number).isSome()).toBe(true); const int2 = Number(2); - expect(Dyn.cast(int2).to().isSome()).toBe(true); + expect(Dyn.cast(int2).to(Number).isSome()).toBe(true); const nonInt = "2"; - expect(Dyn.cast(nonInt).to().isSome()).toBe(true); + expect(Dyn.cast(nonInt).to(Number).isSome()).toBe(false); const foo = new Foo(); const bar = new Bar(); - expect(Dyn.cast(foo).to().isSome()).toBe(true); - expect(Dyn.cast(bar).to().isSome()).toBe(true); - expect(Dyn.cast(foo).to().isSome()).toBe(true); - expect(Dyn.cast(bar).to().isSome()).toBe(true); + expect(Dyn.cast(foo).to(Foo).isSome()).toBe(true); + expect(Dyn.cast(bar).to(Bar).isSome()).toBe(true); + expect(Dyn.cast(foo).to(Bar).isSome()).toBe(false); + expect(Dyn.cast(bar).to(Foo).isSome()).toBe(false); }); test("testing TypeHelpers.stringIsNullishOrEmpty", () => { @@ -120,25 +116,22 @@ test("testing TypeHelpers.stringIsNullishOrWhiteSpace", () => { test("testing Dyn.cast.to exceptions", () => { const strNull = null; - expect(() => Dyn.cast(strNull).to()).toThrowError( - "Invalid" - ); - expect(() => Dyn.cast(strNull).to()).toThrowError( - "parameter" - ); - expect(() => Dyn.cast(strNull).to()).toThrowError( - "null" - ); + expect(() => Dyn.cast(strNull).to(String)).toThrowError("Invalid"); + expect(() => Dyn.cast(strNull).to(String)).toThrowError("parameter"); + expect(() => Dyn.cast(strNull).to(String)).toThrowError("null"); const strUndefined = undefined; - expect(() => Dyn.cast(strUndefined).to()).toThrowError( - "Invalid" - ); - expect(() => Dyn.cast(strUndefined).to()).toThrowError( - "parameter" - ); - expect(() => Dyn.cast(strUndefined).to()).toThrowError( - "undefined" - ); + expect(() => Dyn.cast(strUndefined).to(String)).toThrowError("Invalid"); + expect(() => Dyn.cast(strUndefined).to(String)).toThrowError("parameter"); + expect(() => Dyn.cast(strUndefined).to(String)).toThrowError("undefined"); + + const typeNull = null; + expect(() => Dyn.cast("foo").to(typeNull)).toThrowError("Invalid"); + expect(() => Dyn.cast("foo").to(typeNull)).toThrowError("parameter"); + expect(() => Dyn.cast("foo").to(typeNull)).toThrowError("null"); + const typeUndefined = undefined; + expect(() => Dyn.cast("foo").to(typeUndefined)).toThrowError("Invalid"); + expect(() => Dyn.cast("foo").to(typeUndefined)).toThrowError("parameter"); + expect(() => Dyn.cast("foo").to(typeUndefined)).toThrowError("undefined"); }); function handleResult(result: Result): string {