UNPKG

1.36 kBTypeScriptView Raw
1import type { IObjectOf } from "./object.js";
2/**
3 * Generic interface for reference types (value wrappers).
4 */
5export interface IDeref<T> {
6 /**
7 * Returns wrapped value.
8 */
9 deref(): T;
10}
11export type MaybeDeref<T> = IDeref<T> | T;
12/**
13 * If `T` is a {@link IDeref}, returns its value type or else `T`.
14 */
15export type Derefed<T> = T extends IDeref<any> ? ReturnType<T["deref"]> : T;
16/**
17 * Constructs a type with a set of properties `K` of type `T` and
18 * attempts to resolve each given key via {@link Derefed}.
19 *
20 * @example
21 * ```ts tangle:../export/deref.ts
22 * import type { DerefedKeys, IDeref } from "@thi.ng/api";
23 *
24 * interface Foo {
25 * a: IDeref<string>;
26 * b: IDeref<number>;
27 * c: { d: number };
28 * }
29 *
30 * type Foo2 = DerefedKeys<Foo>;
31 * // { a: string; b: number; c: { d: number; } }
32 *
33 * type Foo3 = DerefedKeys<Foo, "b">;
34 * // { b: number; }
35 * ```
36 */
37export type DerefedKeys<T extends IObjectOf<any>, K extends keyof T = keyof T> = {
38 [P in K]: Derefed<T[P]>;
39};
40/**
41 * Returns true iff `x` implements {@link IDeref}.
42 *
43 * @param x -
44 */
45export declare const isDeref: (x: any) => x is IDeref<any>;
46/**
47 * If `x` implements {@link IDeref}, returns its wrapped value, else
48 * returns `x` itself.
49 *
50 * @param x -
51 */
52export declare const deref: <T>(x: MaybeDeref<T>) => T;
53//# sourceMappingURL=deref.d.ts.map
\No newline at end of file