fp-ts-std
Version:
The missing pseudo-standard library for fp-ts.
144 lines • 4.78 kB
TypeScript
/**
* This module targets readonly objects in the sense of product types. For
* readonly objects in the sense of maps see the `Record` module.
*
* @since 0.14.0
*/
import type * as RR from "fp-ts/ReadonlyRecord";
/**
* Merge two readonly structs together. For merging many identical structs,
* instead consider defining a semigroup.
*
* @example
* import { merge } from 'fp-ts-std/ReadonlyStruct'
*
* assert.deepStrictEqual(merge({ a: 1, b: 2 })({ b: 'two', c: true }), { a: 1, b: 'two', c: true })
*
* @category 3 Functions
* @since 0.14.0
*/
export declare const merge: <A, B>(x: A) => <C extends B>(y: C) => A & C;
/**
* Pick a set of keys from a readonly struct. The value-level equivalent of the
* `Pick` type.
*
* @example
* import { pick } from 'fp-ts-std/ReadonlyStruct'
* 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] })
*
* @category 3 Functions
* @since 0.14.0
*/
export declare const pick: <A extends object, K extends keyof A>(ks: readonly K[]) => (x: A) => Pick<A, K>;
/**
* Like `pick`, but allows you to specify the input struct upfront.
*
* @example
* import { pickFrom } from 'fp-ts-std/ReadonlyStruct'
*
* 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] })
*
* @category 3 Functions
* @since 0.14.0
*/
export declare const pickFrom: <A extends object>() => <K extends keyof A>(ks: readonly K[]) => (x: A) => Pick<A, K>;
/**
* Get the value for a key in a struct.
*
* @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')
*
* @category 3 Functions
* @since 0.17.0
*/
export declare const get: <K extends string>(k: K) => <A>(x: Readonly<Record<K, A>>) => A;
/**
* Omit a set of keys from a readonly struct. The value-level equivalent of the
* `Omit` type.
*
* @example
* import { omit } from 'fp-ts-std/ReadonlyStruct'
*
* const sansB = omit(['b'])
*
* assert.deepStrictEqual(sansB({ a: 1, b: 'two', c: [true] }), { a: 1, c: [true] })
*
* @category 3 Functions
* @since 0.14.0
*/
export declare const omit: <K extends string>(ks: readonly K[]) => <V, A extends Readonly<Record<K, V>>>(x: A) => Omit<A, K>;
/**
* Like `omit`, but allows you to specify the input struct upfront.
*
* @example
* import { omitFrom } from 'fp-ts-std/ReadonlyStruct'
*
* 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] })
*
* @category 3 Functions
* @since 0.15.0
*/
export declare const omitFrom: <A>() => <K extends keyof A & string>(ks: readonly K[]) => (x: A) => Omit<A, K>;
type OptionalKeys<O extends object> = {
[K in keyof O]-?: RR.ReadonlyRecord<string, unknown> extends Pick<O, K> ? K : never;
}[keyof O];
type Exact<A extends Readonly<object>, B extends A> = A & Readonly<Record<Exclude<keyof B, keyof A>, never>>;
/**
* Provide default values for an object with optional properties.
*
* @example
* import { withDefaults } from 'fp-ts-std/ReadonlyStruct'
* 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' })
*
* @category 3 Functions
* @since 0.15.0
*/
export declare const withDefaults: <T extends RR.ReadonlyRecord<string, unknown>, PT extends Exact<{
[K in OptionalKeys<T>]-?: Exclude<T[K], undefined>;
}, PT>>(defaults: PT) => (t: T) => Readonly<PT & T>;
type MaybePartial<A> = A | Partial<A>;
type RenameKey<A extends RR.ReadonlyRecord<string, unknown>, I extends keyof A, J extends string> = Readonly<{
[K in keyof A as K extends I ? J : K]: A[K];
}>;
/**
* Rename a key in a struct, preserving the value. If the new key already
* exists, the old key will be overwritten. Optionality is preserved.
*
* @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')
*
* @category 3 Functions
* @since 0.15.0
*/
export declare const renameKey: <I extends string>(oldK: I) => <J extends string>(newK: J) => <A extends MaybePartial<Readonly<Record<I, unknown>>>>(x: A) => Readonly<{ [K in keyof A as K extends I ? J : K]: A[K]; }>;
export {};
//# sourceMappingURL=ReadonlyStruct.d.ts.map