Struct overview
This module targets objects in the sense of product types. For objects in the sense of maps see the Record
module.
Added in v0.14.0
Table of contents
3 Functions
get
Get the value for a key in a struct.
Signature
export declare const get: <K extends string>(k: K) => <A>(x: Record<K, A>) => A
get :: k extends string => k -> Record k a -> a
Example
import { get } from 'fp-ts-std/Struct'
type Person = { name: string; age: number }
const person: Person = { name: 'Albert', age: 76 }
const getName = get('name')
assert.strictEqual(getName(person), 'Albert')
Added in v0.17.0
merge
Merge two structs together. For merging many identical structs, instead consider defining a semigroup.
Signature
export declare const merge: <A, B>(x: A) => <C extends B>(y: C) => A & C
merge :: c extends b => a -> c -> a & c
Example
import { merge } from 'fp-ts-std/Struct'
assert.deepStrictEqual(merge({ a: 1, b: 2 })({ b: 'two', c: true }), { a: 1, b: 'two', c: true })
Added in v0.14.0
omit
Omit a set of keys from a struct. The value-level equivalent of the Omit
type.
Signature
export declare const omit: <K extends string>(
ks: K[]
) => <V, A extends Record<K, V>>(x: A) => Pick<A, Exclude<keyof A, K>>
omit :: k extends string, a extends (Record k v) => Array k -> a -> Pick a (Exclude (keyof a) k)
Example
import { omit } from 'fp-ts-std/Struct'
const sansB = omit(['b'])
assert.deepStrictEqual(sansB({ a: 1, b: 'two', c: [true] }), { a: 1, c: [true] })
Added in v0.14.0
omitFrom
Like omit
, but allows you to specify the input struct upfront.
Signature
export declare const omitFrom: <A>() => <K extends keyof A & string>(ks: K[]) => (x: A) => Pick<A, Exclude<keyof A, K>>
omitFrom :: k extends ((keyof a) & string) => () -> Array k -> a -> Pick a (Exclude (keyof a) k)
Example
import { omitFrom } from 'fp-ts-std/Struct'
type MyType = { a: number; b: string; c: ReadonlyArray<boolean> }
const sansB = omitFrom<MyType>()(['b'])
assert.deepStrictEqual(sansB({ a: 1, b: 'two', c: [true] }), { a: 1, c: [true] })
Added in v0.15.0
pick
Pick a set of keys from a struct. The value-level equivalent of the Pick
type.
Signature
export declare const pick: <A extends object, K extends keyof A>(ks: K[]) => (x: A) => Pick<A, K>
pick :: a extends object, k extends (keyof a) => Array k -> a -> Pick a k
Example
import { pick } from 'fp-ts-std/Struct'
import { pipe } from 'fp-ts/function'
const picked = pipe({ a: 1, b: 'two', c: [true] }, pick(['a', 'c']))
assert.deepStrictEqual(picked, { a: 1, c: [true] })
Added in v0.14.0
pickFrom
Like pick
, but allows you to specify the input struct upfront.
Signature
export declare const pickFrom: <A extends object>() => <K extends keyof A>(ks: K[]) => (x: A) => Pick<A, K>
pickFrom :: a extends object, k extends (keyof a) => () -> Array k -> a -> Pick a k
Example
import { pickFrom } from 'fp-ts-std/Struct'
type MyType = { a: number; b: string; c: ReadonlyArray<boolean> }
const picked = pickFrom<MyType>()(['a', 'c'])
assert.deepStrictEqual(picked({ a: 1, b: 'two', c: [true] }), { a: 1, c: [true] })
Added in v0.14.0
renameKey
Rename a key in a struct, preserving the value. If the new key already exists, the old key will be overwritten. Optionality is preserved.
Signature
export declare const renameKey: <I extends string>(
oldK: I
) => <J extends string>(newK: J) => <A extends MaybePartial<Record<I, unknown>>>(x: A) => RenameKey<A, I, J>
renameKey :: i extends string, j extends string, a extends (MaybePartial (Record i unknown)) => i -> j -> a -> RenameKey a i j
Example
import { renameKey } from 'fp-ts-std/Struct'
type Foo = { a: string; b: number }
type Bar = { a: string; c: number }
const fooBar: (x: Foo) => Bar = renameKey('b')('c')
Added in v0.15.0
withDefaults
Provide default values for an object with optional properties.
Signature
export declare const withDefaults: <
T extends object,
PT extends Exact<{ [K in OptionalKeys<T>]-?: Exclude<T[K], undefined> }, PT>
>(
defaults: PT
) => (t: T) => PT & T
Example
import { withDefaults } from 'fp-ts-std/Struct'
import { pipe } from 'fp-ts/function'
const aOptB: { a: number; b?: string } = { a: 1 }
assert.deepStrictEqual(pipe(aOptB, withDefaults({ b: 'foo' })), { a: 1, b: 'foo' })
Added in v0.15.0