<P extends PropertyKey>(property: P): (
self: unknown
) => self is { [K in P]: unknown }
<P extends PropertyKey>(self: unknown, property: P): self is {
[K in P]: unknown
}Checks whether a value has a given property key.
When to use
Use when you need a Predicate guard for property access on unknown
values with a simple structural object check.
Details
Uses the in operator and isObjectKeyword. This does not check property
value types.
Example (Guarding object properties)
import { Predicate } from "effect"
const hasName = Predicate.hasProperty("name")
const data: unknown = { name: "Ada" }
if (hasName(data)) {
console.log(data.name)
}export const const hasProperty: {
<P extends PropertyKey>(property: P): (
self: unknown
) => self is { [K in P]: unknown }
<P extends PropertyKey>(
self: unknown,
property: P
): self is { [K in P]: unknown }
}
Checks whether a value has a given property key.
When to use
Use when you need a Predicate guard for property access on unknown
values with a simple structural object check.
Details
Uses the in operator and isObjectKeyword. This does not check property
value types.
Example (Guarding object properties)
import { Predicate } from "effect"
const hasName = Predicate.hasProperty("name")
const data: unknown = { name: "Ada" }
if (hasName(data)) {
console.log(data.name)
}
hasProperty: {
<function (type parameter) P in <P extends PropertyKey>(property: P): (self: unknown) => self is { [K in P]: unknown; }P extends type PropertyKey =
| string
| number
| symbol
PropertyKey>(property: P extends PropertyKeyproperty: function (type parameter) P in <P extends PropertyKey>(property: P): (self: unknown) => self is { [K in P]: unknown; }P): (self: unknownself: unknown) => self: unknownself is { [function (type parameter) KK in function (type parameter) P in <P extends PropertyKey>(property: P): (self: unknown) => self is { [K in P]: unknown; }P]: unknown }
<function (type parameter) P in <P extends PropertyKey>(self: unknown, property: P): self is { [K in P]: unknown; }P extends type PropertyKey =
| string
| number
| symbol
PropertyKey>(self: unknownself: unknown, property: P extends PropertyKeyproperty: function (type parameter) P in <P extends PropertyKey>(self: unknown, property: P): self is { [K in P]: unknown; }P): self: unknownself is { [function (type parameter) KK in function (type parameter) P in <P extends PropertyKey>(self: unknown, property: P): self is { [K in P]: unknown; }P]: unknown }
} = dual<(...args: Array<any>) => any, <P extends PropertyKey>(self: unknown, property: P) => self is { [K in P]: unknown; }>(arity: 2, body: <P extends PropertyKey>(self: unknown, property: P) => self is { [K in P]: unknown; }): ((...args: Array<any>) => any) & (<P extends PropertyKey>(self: unknown, property: P) => self is { [K in P]: unknown; }) (+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) P in <P extends PropertyKey>(self: unknown, property: P): self is { [K in P]: unknown; }P extends type PropertyKey =
| string
| number
| symbol
PropertyKey>(self: unknownself: unknown, property: P extends PropertyKeyproperty: function (type parameter) P in <P extends PropertyKey>(self: unknown, property: P): self is { [K in P]: unknown; }P): self: unknownself is { [function (type parameter) KK in function (type parameter) P in <P extends PropertyKey>(self: unknown, property: P): self is { [K in P]: unknown; }P]: unknown } =>
function isObjectKeyword(
input: unknown
): input is object
Checks whether a value is an object in the JavaScript sense (objects, arrays, functions).
When to use
Use when you need a Predicate guard that accepts arrays and functions as
well as objects.
Details
Returns true for arrays and functions, and false for null.
Example (Checking object keywords)
import { Predicate } from "effect"
console.log(Predicate.isObjectKeyword(() => 1))
console.log(Predicate.isObjectKeyword(null))
isObjectKeyword(self: unknownself) && (property: P extends PropertyKeyproperty in self: objectself)
)