SafeRefinement<string, never>Matches non-empty strings.
When to use
Use to match strings whose length is greater than zero.
Details
This predicate matches any string that contains at least one character, effectively filtering out empty strings ("").
Example (Matching non-empty strings)
import { Match } from "effect"
const processInput = Match.type<string>()
.pipe(
Match.when(Match.nonEmptyString, (str) => `Valid input: ${str}`),
Match.orElse(() => "Input cannot be empty")
)
console.log(processInput("hello"))
// Output: "Valid input: hello"
console.log(processInput(""))
// Output: "Input cannot be empty"
console.log(processInput(" "))
// Output: "Valid input: " (whitespace-only strings are considered non-empty)export const const nonEmptyString: SafeRefinement<
string,
never
>
Matches non-empty strings.
When to use
Use to match strings whose length is greater than zero.
Details
This predicate matches any string that contains at least one character,
effectively filtering out empty strings ("").
Example (Matching non-empty strings)
import { Match } from "effect"
const processInput = Match.type<string>()
.pipe(
Match.when(Match.nonEmptyString, (str) => `Valid input: ${str}`),
Match.orElse(() => "Input cannot be empty")
)
console.log(processInput("hello"))
// Output: "Valid input: hello"
console.log(processInput(""))
// Output: "Input cannot be empty"
console.log(processInput(" "))
// Output: "Valid input: " (whitespace-only strings are considered non-empty)
nonEmptyString: interface SafeRefinement<in A, out R = A>A safe refinement that narrows types without runtime errors.
Details
SafeRefinement provides a way to refine types in pattern matching while
maintaining type safety. Unlike regular predicates, safe refinements can
transform the matched value's type without throwing runtime errors.
Example (Using safe refinements)
import { Match } from "effect"
// Built-in safe refinements
const processValue = Match.type<unknown>().pipe(
Match.when(Match.string, (s) => s.toUpperCase()),
Match.when(Match.number, (n) => n * 2),
Match.when(Match.defined, (value) => `Defined: ${value}`),
Match.orElse(() => "Undefined or null")
)
console.log(processValue("hello")) // "HELLO"
console.log(processValue(21)) // 42
console.log(processValue(true)) // "Defined: true"
console.log(processValue(null)) // "Undefined or null"
SafeRefinement<string, never> = import internalinternal.const nonEmptyString: SafeRefinement<
string,
never
>
nonEmptyString