ts-flex-query
Version:
Flexible and type-safe data queries
29 lines (28 loc) • 2.64 kB
TypeScript
import { IfAny } from '../types/utils';
/** Creates an object from an array with the given key and value getters. */
export declare function createObjectFromArray<TElement, TKey extends PropertyKey, TValue>(array: TElement[], keyGetter: (element: TElement) => TKey, valueGetter: (element: TElement) => TValue): Record<TKey, TValue>;
/** Creates an object from an array with the given key getter where the value equals the respective element. */
export declare function createObjectFromArray<TElement, TKey extends PropertyKey>(array: TElement[], keyGetter: (element: TElement) => TKey): Record<TKey, TElement>;
/** Creates a new object from the given object by rewriting values. */
export declare function createObjectFromObject<TKey extends PropertyKey, TOldValue, TNewValue>(obj: Record<TKey, Exclude<TOldValue, undefined>>, valueGetter: (value: Exclude<TOldValue, undefined>, key: TKey) => TNewValue): Record<TKey, TNewValue>;
/** Creates a new object from the given object by rewriting values. */
export declare function createObjectFromObject<TKey extends PropertyKey, TOldValue, TNewValue>(obj: Partial<Record<TKey, TOldValue>>, valueGetter: (value: TOldValue, key: TKey) => TNewValue): Partial<Record<TKey, TNewValue>>;
/** Creates a new object from the given object by rewriting keys and values. */
export declare function createObjectFromObject<TOldKey extends PropertyKey, TOldValue, TNewKey extends PropertyKey, TNewValue>(obj: Partial<Record<TOldKey, TOldValue>>, valueGetter: (value: TOldValue, key: TOldKey) => TNewValue, keyGetter: (key: TOldKey) => TNewKey): Partial<Record<TNewKey, TNewValue>>;
/** Produces a test function to check if a value is of a given type. */
export declare function typePredicate<T, U extends T>(f: (value: T) => boolean): (value: T) => value is U;
/**
* Throws an error when called.
* Used to mark code that must not be executed.
*/
export declare function unexpected(x: never): never;
/**
* Creates a type expector method.
* Its first argument is the value to test.
* The second argument must be true.
* The assertion fails if one of T and U is "any" but not both or if U does not extend T.
*/
export declare const expectType: <T>() => <U extends T>(_value: U, _shouldMatch: IfAny<U, IfAny<T, true, 'Value must not have type any.'>, IfAny<T, 'Value must have type any.', true>>) => void;
export declare const nameOf: <T>() => <TField extends keyof T>(field: TField) => TField;
export declare function isDefined<T>(value: T): value is Exclude<T, undefined | null>;
export declare function assertIsDefined<T>(value: T, errorMessage: string): asserts value is Exclude<T, undefined | null>;