diff --git a/src/types.ts b/src/types.ts index a3546db9..68e482c0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -138,6 +138,27 @@ export type PositiveFloat = IfEquals< never >; +/** + * Represents a positive float parsed from a string. + * If the string does not represent a positive float, it resolves to `never`, else + * it resolves to its float representation. + * @example + * ````ts + PositiveFloatString<'0'>; // never + PositiveFloatString<'82739283293237'>; // works + PositiveFloatString<'-82739.283293237'>; // never + PositiveFloatString<'-1'>; // never + PositiveFloatString<'1.98'>; // works + PositiveFloatString<'-1.98'>; // never + * ```` + */ +export type PositiveFloatString = IfEquals< + IsPositiveFloat>>, + true, + Float>, + never +>; + /** * Type representing a float that's in ]-∞, 0[ */ @@ -167,6 +188,7 @@ export type NegativeFloatString = IfEquals< Float>, never >; + /** * Is it a negative float ? * @return diff --git a/tests/negative-float-string.test.ts b/tests/negative-float-string.test.ts index a20538e6..14b2a938 100644 --- a/tests/negative-float-string.test.ts +++ b/tests/negative-float-string.test.ts @@ -33,4 +33,3 @@ test(() => { const result: TestType, never, true> = true; expect(result).toBe(true); }); - diff --git a/tests/positive-float-string.test.ts b/tests/positive-float-string.test.ts new file mode 100644 index 00000000..0f4f8892 --- /dev/null +++ b/tests/positive-float-string.test.ts @@ -0,0 +1,39 @@ +import { PositiveFloatString, TestType } from '@/types'; +import { test, expect } from 'vitest'; + +test(() => { + const result: TestType, never, true> = true; + expect(result).toBe(true); +}); + +test(() => { + const result: TestType< + PositiveFloatString<'-82739283293237'>, + never, + true + > = true; + expect(result).toBe(true); +}); + +test(() => { + const result: TestType< + PositiveFloatString<'82739.283293237'>, + 82739.283293237, + true + > = true; + expect(result).toBe(true); +}); + +test(() => { + const result: TestType, never, true> = true; + expect(result).toBe(true); +}); + +test(() => { + const result: TestType, 0.54, true> = true; + expect(result).toBe(true); +}); +test(() => { + const result: TestType, never, true> = true; + expect(result).toBe(true); +});