LendAndRegret/node_modules/es-toolkit/dist/compat/array/sample.d.ts
2026-05-02 17:27:43 +08:00

39 lines
1.1 KiB
TypeScript

/**
* Gets a random element from collection.
*
* @template T
* @param {readonly [T, ...T[]]} collection - The collection to sample.
* @returns {T} Returns the random element.
*
* @example
* sample([1, 2, 3, 4]);
* // => 2
*/
declare function sample<T>(collection: readonly [T, ...T[]]): T;
/**
* Gets a random element from collection.
*
* @template T
* @param {Record<string, T> | Record<number, T> | null | undefined} collection - The collection to sample.
* @returns {T | undefined} Returns the random element.
*
* @example
* sample({ 'a': 1, 'b': 2, 'c': 3 });
* // => 2
*/
declare function sample<T>(collection: Record<string, T> | Record<number, T> | null | undefined): T | undefined;
/**
* Gets a random element from collection.
*
* @template T
* @param {T | null | undefined} collection - The collection to sample.
* @returns {T[keyof T] | undefined} Returns the random element.
*
* @example
* sample({ 'a': 1, 'b': 2, 'c': 3 });
* // => 2
*/
declare function sample<T extends object>(collection: T | null | undefined): T[keyof T] | undefined;
export { sample };