<S extends object, const Keys extends ReadonlyArray<keyof S>>(
keys: Keys
): (self: S) => Simplify<Pick<S, Keys[number]>>
<S extends object, const Keys extends ReadonlyArray<keyof S>>(
self: S,
keys: Keys
): Simplify<Pick<S, Keys[number]>>Creates a new struct containing only the specified keys.
When to use
Use to narrow a struct down to a subset of its properties.
Gotchas
Keys not present in the struct are silently ignored.
Example (Selecting specific properties)
import { pipe, Struct } from "effect"
const user = { name: "Alice", age: 30, admin: true }
const nameAndAge = pipe(user, Struct.pick(["name", "age"]))
console.log(nameAndAge) // { name: "Alice", age: 30 }export const const pick: {
<
S extends object,
Keys extends ReadonlyArray<keyof S>
>(
keys: Keys
): (self: S) => Simplify<Pick<S, Keys[number]>>
<
S extends object,
Keys extends ReadonlyArray<keyof S>
>(
self: S,
keys: Keys
): Simplify<Pick<S, Keys[number]>>
}
Creates a new struct containing only the specified keys.
When to use
Use to narrow a struct down to a subset of its properties.
Gotchas
Keys not present in the struct are silently ignored.
Example (Selecting specific properties)
import { pipe, Struct } from "effect"
const user = { name: "Alice", age: 30, admin: true }
const nameAndAge = pipe(user, Struct.pick(["name", "age"]))
console.log(nameAndAge) // { name: "Alice", age: 30 }
pick: {
<function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(keys: Keys): (self: S) => Simplify<Pick<S, Keys[number]>>S extends object, const function (type parameter) Keys in <S extends object, const Keys extends ReadonlyArray<keyof S>>(keys: Keys): (self: S) => Simplify<Pick<S, Keys[number]>>Keys extends interface ReadonlyArray<T>ReadonlyArray<keyof function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(keys: Keys): (self: S) => Simplify<Pick<S, Keys[number]>>S>>(
keys: const Keys extends ReadonlyArray<keyof S>keys: function (type parameter) Keys in <S extends object, const Keys extends ReadonlyArray<keyof S>>(keys: Keys): (self: S) => Simplify<Pick<S, Keys[number]>>Keys
): (self: S extends objectself: function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(keys: Keys): (self: S) => Simplify<Pick<S, Keys[number]>>S) => type Simplify<T> = {
[K in keyof T]: T[K]
}
Flattens intersection types into a single object type for readability.
When to use
Use when hovering over a type shows A & B & C instead of the merged shape.
Details
This helper is purely cosmetic at the type level and has no runtime effect.
It preserves readonly modifiers; use
Mutable
to strip them.
Example (Flattening an intersection)
import type { Struct } from "effect"
type Original = { a: string } & { b: number }
// Without Simplify, the type displays as `{ a: string } & { b: number }`
type Simplified = Struct.Simplify<Original>
// { a: string; b: number }
Simplify<type Pick<T, K extends keyof T> = {
[P in K]: T[P]
}
From T, pick a set of properties whose keys are in the union K
Pick<function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(keys: Keys): (self: S) => Simplify<Pick<S, Keys[number]>>S, function (type parameter) Keys in <S extends object, const Keys extends ReadonlyArray<keyof S>>(keys: Keys): (self: S) => Simplify<Pick<S, Keys[number]>>Keys[number]>>
<function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): Simplify<Pick<S, Keys[number]>>S extends object, const function (type parameter) Keys in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): Simplify<Pick<S, Keys[number]>>Keys extends interface ReadonlyArray<T>ReadonlyArray<keyof function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): Simplify<Pick<S, Keys[number]>>S>>(self: S extends objectself: function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): Simplify<Pick<S, Keys[number]>>S, keys: const Keys extends ReadonlyArray<keyof S>keys: function (type parameter) Keys in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): Simplify<Pick<S, Keys[number]>>Keys): type Simplify<T> = {
[K in keyof T]: T[K]
}
Flattens intersection types into a single object type for readability.
When to use
Use when hovering over a type shows A & B & C instead of the merged shape.
Details
This helper is purely cosmetic at the type level and has no runtime effect.
It preserves readonly modifiers; use
Mutable
to strip them.
Example (Flattening an intersection)
import type { Struct } from "effect"
type Original = { a: string } & { b: number }
// Without Simplify, the type displays as `{ a: string } & { b: number }`
type Simplified = Struct.Simplify<Original>
// { a: string; b: number }
Simplify<type Pick<T, K extends keyof T> = {
[P in K]: T[P]
}
From T, pick a set of properties whose keys are in the union K
Pick<function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): Simplify<Pick<S, Keys[number]>>S, function (type parameter) Keys in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): Simplify<Pick<S, Keys[number]>>Keys[number]>>
} = import dualdual(
2,
<function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): anyS extends object, const function (type parameter) Keys in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): anyKeys extends interface ReadonlyArray<T>ReadonlyArray<keyof function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): anyS>>(self: S extends objectself: function (type parameter) S in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): anyS, keys: const Keys extends ReadonlyArray<keyof S>keys: function (type parameter) Keys in <S extends object, const Keys extends ReadonlyArray<keyof S>>(self: S, keys: Keys): anyKeys) => {
return function buildStruct<S, (k: keyof S, v: S[keyof S]) => [keyof S, S[keyof S]] | undefined>(source: S, f: (k: keyof S, v: S[keyof S]) => [keyof S, S[keyof S]] | undefined): anyWalk source; for each key decide what to emit via the small callback.
The callback returns either
• undefined → nothing is copied, or
• [newKey, newVal]
so every public API just supplies a different callback.
buildStruct(self: S extends objectself, (k: keyof Sk, v: S[keyof S]v) => (keys: const Keys extends ReadonlyArray<keyof S>keys.ReadonlyArray<keyof S>.includes(searchElement: keyof S, fromIndex?: number): booleanDetermines whether an array includes a certain element, returning true or false as appropriate.
includes(k: keyof Sk) ? [k: keyof Sk, v: S[keyof S]v] : var undefinedundefined))
}
)