UNPKG

space-lift

Version:

TypeScript Array, Object, Map, Set, Union, Enum utils

62 lines (61 loc) 2.42 kB
import { ArrayWrapper } from './array'; import { MapWrapper } from './map'; import type { Pipe } from './lift'; export declare class ObjectWrapper<T extends object> { private _value; constructor(_value: T); private _isLiftWrapper; value(): T; private _clone; /** * Adds a new key/value to this object. This creates a new type. * To add a nullable key to an object while preserving its type, use "update()" instead. */ add<K extends string, V>(key: K, value: V): ObjectWrapper<T & { [P in K]: V; }>; /** * Returns whether this object contains no keys. */ isEmpty(): boolean; /** * Creates an Array of all this object's keys, in no particular order. * If the keys are a subtype of string, the Array will be typed with the proper key union type. */ keys(): ArrayWrapper<Array<KeyAsString<keyof T>>>; /** * Maps one of this Object values, by key. * This is similar to remove('key').add('key', newValue) but is less error prone. * This can change the type of the object. */ mapValue<K extends keyof T, V>(key: K, mapFunction: (value: T[K]) => V): ObjectWrapper<{ [K2 in keyof T]: K2 extends K ? V : T[K2]; }>; /** * Maps this Object's values. * This is mostly useful for objects with a single value type. */ mapValues<V>(mapFunction: (value: T[keyof T]) => V): ObjectWrapper<Record<keyof T, V>>; pipe: typeof import("./lift").pipe; /** * Removes a key/value from this object and return a new object (and type) * To delete a (nullable) key from an object while preserving its type, use "update()" instead. */ remove<K extends keyof T>(keyToRemove: K): ObjectWrapper<Omit<T, K>>; /** * Creates an Array with all these object's values. */ values(): ArrayWrapper<Array<T[keyof T]>>; /** * Converts this Object to an Array of tuples. * Similar to Object.entries() but retains the type of keys. */ toArray(): ArrayWrapper<[KeyAsString<keyof T>, T[keyof T]][]>; /** * Transforms this Object to a Map where the keys are the string typed keys of this Object. */ toMap(): MapWrapper<KeyAsString<keyof T>, T[keyof T], Map<KeyAsString<keyof T>, T[keyof T]>>; } export declare function setObjectPipe(_pipe: Pipe): void; declare type KeyAsString<K> = K extends string ? K : string; export {};