(s: string): Option.Option<number>Parses a number from a string safely using the Number() function.
The following special string values are supported: "NaN", "Infinity", "-Infinity".
When to use
Use to parse numeric text without throwing on invalid input.
Example (Parsing numbers from strings)
import { Number } from "effect"
Number.parse("42") // Option.some(42)
Number.parse("3.14") // Option.some(3.14)
Number.parse("NaN") // Option.some(NaN)
Number.parse("Infinity") // Option.some(Infinity)
Number.parse("-Infinity") // Option.some(-Infinity)
Number.parse("not a number") // Option.none()export const const parse: (
s: string
) => Option.Option<number>
Parses a number from a string safely using the Number() function.
The following special string values are supported: "NaN", "Infinity", "-Infinity".
When to use
Use to parse numeric text without throwing on invalid input.
Example (Parsing numbers from strings)
import { Number } from "effect"
Number.parse("42") // Option.some(42)
Number.parse("3.14") // Option.some(3.14)
Number.parse("NaN") // Option.some(NaN)
Number.parse("Infinity") // Option.some(Infinity)
Number.parse("-Infinity") // Option.some(-Infinity)
Number.parse("not a number") // Option.none()
parse = (s: strings: string): import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<number> => {
if (s: strings === "NaN") {
return import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(var NaN: numberNaN)
}
if (s: strings === "Infinity") {
return import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(var Infinity: numberInfinity)
}
if (s: strings === "-Infinity") {
return import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(-var Infinity: numberInfinity)
}
if (s: strings.String.trim(): stringRemoves the leading and trailing white space and line terminator characters from a string.
trim() === "") {
return import OptionOption.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none()
}
const const n: numbern = const Number: NumberConstructor
;(value?: any) => number
Exposes the global number constructor.
When to use
Use to access native JavaScript numeric coercion from the Effect module
namespace.
Gotchas
This follows native Number coercion rules, including empty strings
becoming 0 and invalid numeric strings becoming NaN.
Number(s: strings)
return const Number: NumberConstructorExposes the global number constructor.
When to use
Use to access native JavaScript numeric coercion from the Effect module
namespace.
Gotchas
This follows native Number coercion rules, including empty strings
becoming 0 and invalid numeric strings becoming NaN.
Number.NumberConstructor.isNaN(number: unknown): booleanReturns a Boolean value that indicates whether a value is the reserved value NaN (not a
number). Unlike the global isNaN(), Number.isNaN() doesn't forcefully convert the parameter
to a number. Only values of the type number, that are also NaN, result in true.
isNaN(const n: numbern) ? import OptionOption.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none() : import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(const n: numbern)
}