(a: unknown): a is DateMatches values that are instances of Date.
When to use
Use to match Date instances.
Details
This predicate refines unknown values to Date instances, allowing pattern matching on Date objects. It only matches actual Date instances, not date strings or timestamps.
Example (Matching Date instances)
import { Match } from "effect"
const processDateValue = Match.type<unknown>().pipe(
Match.when(Match.date, (date) => {
if (isNaN(date.getTime())) {
return "Invalid date"
}
return `Date: ${date.toISOString().split("T")[0]}`
}),
Match.when(Match.string, (str) => `Date string: ${str}`),
Match.orElse(() => "Not a date-related value")
)
console.log(processDateValue(new Date("2024-01-01"))) // "Date: 2024-01-01"
console.log(processDateValue(new Date("invalid"))) // "Invalid date"
console.log(processDateValue("2024-01-01")) // "Date string: 2024-01-01"
console.log(processDateValue(1704067200000)) // "Not a date-related value"export const const date: Predicate.Refinement<
unknown,
Date
>
Matches values that are instances of Date.
When to use
Use to match Date instances.
Details
This predicate refines unknown values to Date instances, allowing pattern
matching on Date objects. It only matches actual Date instances, not
date strings or timestamps.
Example (Matching Date instances)
import { Match } from "effect"
const processDateValue = Match.type<unknown>().pipe(
Match.when(Match.date, (date) => {
if (isNaN(date.getTime())) {
return "Invalid date"
}
return `Date: ${date.toISOString().split("T")[0]}`
}),
Match.when(Match.string, (str) => `Date string: ${str}`),
Match.orElse(() => "Not a date-related value")
)
console.log(processDateValue(new Date("2024-01-01"))) // "Date: 2024-01-01"
console.log(processDateValue(new Date("invalid"))) // "Invalid date"
console.log(processDateValue("2024-01-01")) // "Date string: 2024-01-01"
console.log(processDateValue(1704067200000)) // "Not a date-related value"
date: import PredicatePredicate.interface Refinement<in A, out B extends A>A predicate that also narrows the input type when it returns true.
When to use
Use when you want a runtime check that refines A to B for TypeScript,
especially when composing type guards with
compose
or safely
checking unknown values.
Details
A refinement returns a type predicate (a is B). Use it with if or
filter to narrow types.
Example (Narrowing unknown values)
import { Predicate } from "effect"
const isString: Predicate.Refinement<unknown, string> = (u): u is string => typeof u === "string"
const data: unknown = "hello"
if (isString(data)) {
console.log(data.toUpperCase())
}
Type-level utilities for working with
Refinement
types.
When to use
Use when you need to extract input and output types from refinement
signatures while writing generic helpers over refinements.
Details
These utilities are type-only, create no runtime values, and the namespace is
erased at runtime.
Example (Extracting refinement types)
import { Predicate } from "effect"
type IsString = Predicate.Refinement<unknown, string>
type Input = Predicate.Refinement.In<IsString>
type Output = Predicate.Refinement.Out<IsString>
Refinement<unknown, Date> = import PredicatePredicate.function isDate(
input: unknown
): input is Date
Checks whether a value is a Date.
When to use
Use when you need a Predicate runtime guard for dates.
Details
Uses instanceof Date.
Example (Guarding Date values)
import { Predicate } from "effect"
const data: unknown = new Date()
console.log(Predicate.isDate(data))
isDate