<K extends string>(tag: K): (self: unknown) => self is { _tag: K }
<K extends string>(self: unknown, tag: K): self is { _tag: K }Checks whether a value has a _tag property equal to the given tag.
When to use
Use when you model tagged unions with a _tag field and want a quick
Predicate guard for tagged values.
Details
Uses hasProperty and strict equality on _tag.
Example (Guarding tagged values)
import { Predicate } from "effect"
const isOk = Predicate.isTagged("Ok")
console.log(isOk({ _tag: "Ok", value: 1 }))export const const isTagged: {
<K extends string>(tag: K): (
self: unknown
) => self is {
_tag: K
}
<K extends string>(
self: unknown,
tag: K
): self is {
_tag: K
}
}
Checks whether a value has a _tag property equal to the given tag.
When to use
Use when you model tagged unions with a _tag field and want a quick
Predicate guard for tagged values.
Details
Uses hasProperty and strict equality on _tag.
Example (Guarding tagged values)
import { Predicate } from "effect"
const isOk = Predicate.isTagged("Ok")
console.log(isOk({ _tag: "Ok", value: 1 }))
isTagged: {
<function (type parameter) K in <K extends string>(tag: K): (self: unknown) => self is {
_tag: K;
}
K extends string>(tag: K extends stringtag: function (type parameter) K in <K extends string>(tag: K): (self: unknown) => self is {
_tag: K;
}
K): (self: unknownself: unknown) => self: unknownself is { _tag: K extends string_tag: function (type parameter) K in <K extends string>(tag: K): (self: unknown) => self is {
_tag: K;
}
K }
<function (type parameter) K in <K extends string>(self: unknown, tag: K): self is {
_tag: K;
}
K extends string>(self: unknownself: unknown, tag: K extends stringtag: function (type parameter) K in <K extends string>(self: unknown, tag: K): self is {
_tag: K;
}
K): self: unknownself is { _tag: K extends string_tag: function (type parameter) K in <K extends string>(self: unknown, tag: K): self is {
_tag: K;
}
K }
} = dual<(...args: Array<any>) => any, <K extends string>(self: unknown, tag: K) => self is {
_tag: K;
}>(arity: 2, body: <K extends string>(self: unknown, tag: K) => self is {
_tag: K;
}): ((...args: Array<any>) => any) & (<K extends string>(self: unknown, tag: K) => self is {
_tag: K;
}) (+1 overload)
Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(
2,
<function (type parameter) K in <K extends string>(self: unknown, tag: K): self is {
_tag: K;
}
K extends string>(self: unknownself: unknown, tag: K extends stringtag: function (type parameter) K in <K extends string>(self: unknown, tag: K): self is {
_tag: K;
}
K): self: unknownself is { _tag: K extends string_tag: function (type parameter) K in <K extends string>(self: unknown, tag: K): self is {
_tag: K;
}
K } => const hasProperty: <"_tag">(self: unknown, property: "_tag") => self is { [K in "_tag"]: unknown; } (+1 overload)hasProperty(self: unknownself, "_tag") && self: {
_tag: unknown
}
self["_tag"] === tag: K extends stringtag
)