@ts-rust/std
Version:
Rust-inspired utilities for TypeScript: Option, Result, and error handling for safer, more predictable code.
153 lines • 5.25 kB
TypeScript
import type { Option, PendingOption } from "./interface";
/**
* Creates a {@link Some} variant of an {@link Option} containing the given value.
*
* Wraps the provided value in a {@link Some}, indicating the presence of a value.
*
* @template T - The type of the value.
* @param value - The value to wrap in {@link Some}.
* @returns An {@link Option} containing the value as {@link Some}.
*
* @example
* ```ts
* const x = some(42);
*
* expect(x.isSome()).toBe(true);
* expect(x.expect("Not 42")).toBe(42);
* ```
*/
export declare function some<T>(value: T): Option<T>;
/**
* Creates a {@link None} variant of an {@link Option}, representing the absence
* of a value.
*
* Produces an option indicating no value is present.
*
* @template T - The type of the absent value.
* @returns An {@link Option} representing {@link None}.
*
* @example
* ```ts
* const x = none<number>();
*
* expect(x.isNone()).toBe(true);
* expect(() => x.expect("x is `None`")).toThrow("x is `None`");
* ```
*/
export declare function none<T>(): Option<T>;
/**
* Creates a {@link PendingOption | PendingOption\<T>} that resolves to
* {@link Some} containing the awaited value.
*
* Takes a value or a promise and wraps its resolved result in a {@link Some},
* ensuring the value type is `Awaited` to handle any `PromiseLike` input.
*
* @template T - The type of the input value or promise.
* @param value - The value or promise to wrap in {@link Some}.
* @returns A {@link PendingOption} resolving to {@link Some} with the awaited value.
*
* @example
* ```ts
* const x = pendingSome(42);
* const y = pendingSome(Promise.resolve("hello"));
*
* expect(await x).toStrictEqual(some(42));
* expect(await y).toStrictEqual(some("hello"));
* ```
*/
export declare function pendingSome<T>(value: T | Promise<T>): PendingOption<Awaited<T>>;
/**
* Creates a {@link PendingOption | PendingOption\<T>} that resolves to {@link None}.
*
* Produces a pending option representing the absence of a value, with the type
* resolved to `Awaited` for consistency with asynchronous operations.
*
* @template T - The type of the absent value.
* @returns A {@link PendingOption} resolving to {@link None}.
*
* @example
* ```ts
* const x = pendingNone<number>();
*
* expect(await x).toStrictEqual(none());
* expect((await x).isNone()).toBe(true);
* ```
*/
export declare function pendingNone<T>(): PendingOption<Awaited<T>>;
/**
* Creates a {@link PendingOption | PendingOption\<T>} from an option, promise,
* or factory function.
*
* Accepts an {@link Option}, a `Promise` resolving to an {@link Option}, or
* a function returning either, and converts it into a pending option, handling
* asynchronous resolution as needed.
*
* @template T - The type of the value in the option.
* @param optionOrFactory - The {@link Option}, promise, or factory function producing an {@link Option}.
* @returns A {@link PendingOption} resolving to the provided or produced option.
*
* @example
* ```ts
* const x = pendingOption(some(42));
* const y = pendingOption(() => Promise.resolve(none<string>()));
* const z = pendingOption(async () => some("thing"));
*
* expect(await x).toStrictEqual(some(42));
* expect(await y).toStrictEqual(none());
* expect(await z).toStrictEqual(some("thing"));
* ```
*/
export declare function pendingOption<T>(optionOrFactory: Option<T> | Promise<Option<T>> | (() => Option<T> | Promise<Option<T>>)): PendingOption<T>;
/**
* Checks if a value is an {@link Option}, narrowing its type to `Option<unknown>`.
*
* This type guard verifies whether the input conforms to the {@link Optional}
* interface, indicating it is either a {@link Some} or {@link None}.
*
* @param x - The value to check.
* @returns `true` if the value is an {@link Option}, narrowing to `Option<unknown>`.
*
* @example
* ```ts
* const x: unknown = some(42);
* const y: unknown = none<number>();
* const z: unknown = "not an option";
*
* expect(isOption(x)).toBe(true);
* expect(isOption(y)).toBe(true);
* expect(isOption(z)).toBe(false);
*
* if (isOption(x)) {
* expect(x.isSome()).toBe(true); // Type narrowed to Option<unknown>
* }
* ```
*/
export declare function isOption(x: unknown): x is Option<unknown>;
/**
* Checks if a value is a {@link PendingOption}, narrowing its type to
* `PendingOption<unknown>`.
*
* This type guard verifies whether the input is a {@link PendingOption},
* indicating it wraps a `Promise` resolving to an {@link Option}
* (either {@link Some} or {@link None}).
*
* @param x - The value to check.
* @returns `true` if the value is a {@link PendingOption}, narrowing to `PendingOption<unknown>`.
*
* @example
* ```ts
* const x: unknown = pendingOption(some(42));
* const y: unknown = pendingOption(none<number>());
* const z: unknown = some(42); // Not a PendingOption
*
* expect(isPendingOption(x)).toBe(true);
* expect(isPendingOption(y)).toBe(true);
* expect(isPendingOption(z)).toBe(false);
*
* if (isPendingOption(x)) {
* expect(await x).toStrictEqual(some(42)); // Type narrowed to PendingOption<unknown>
* }
* ```
*/
export declare function isPendingOption(x: unknown): x is PendingOption<unknown>;
//# sourceMappingURL=option.d.ts.map