UNPKG

rivo

Version:

🤖 The ultimate library you need for composable type-level programming in TypeScript, powered by HKT.

35 lines (32 loc) • 1.2 kB
import type { Int } from "."; import type { ParsingError } from "../../Error"; import type { Args, Call1, Fn } from "../../HKT"; import type { Every } from "../../List/Every"; import type { Err, Ok, Result } from "../../Result"; import type IsDigitFn from "../../Str/IsDigit"; import type { Str$$Show } from "../../Str/Show"; import type { ToChars } from "../../Str/ToChars"; import type { Assert } from "../../helpers"; import type { Abs } from "../Abs"; /** * Parse a string to an {@link Int}. * * Sig: `(s: string) => Result<Int, ParsingError>` */ export type FromStr<S extends string> = Assert< Result<Int, ParsingError>, string extends S ? Result<Int, ParsingError> : S extends `${infer N extends number}` ? Every<IsDigitFn, ToChars<`${Abs<N>}`>> extends true ? Ok<N> : Err<ParsingError<`${Call1<Str$$Show["Show"], S>} is not a valid integer`>> : Err<ParsingError<`${Call1<Str$$Show["Show"], S>} is not a valid integer`>> >; /** * [Fn] Parse a string to an {@link Int}. * * Sig: `(s: string) => Result<Int, ParsingError>` */ export default interface FromStrFn extends Fn<[string], Result<Int, ParsingError>> { def: ([s]: Args<this>) => FromStr<typeof s>; }