mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
fix(min, max): Do not return undefined
in non-empty arrays (#270)
* fix: improve min&max type * Apply suggestions from code review --------- Co-authored-by: Sojin Park <raon0211@gmail.com>
This commit is contained in:
parent
999a4daf87
commit
d117481dc4
@ -11,7 +11,9 @@
|
|||||||
## 인터페이스
|
## 인터페이스
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function max<T>(items: T[]): T;
|
function max<T>(items: [T, ...T[]]): T;
|
||||||
|
function max(): undefined;
|
||||||
|
function max<T>(items?: T[]): T | undefined;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 파라미터
|
### 파라미터
|
||||||
|
@ -11,7 +11,9 @@
|
|||||||
## 인터페이스
|
## 인터페이스
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function min<T>(items: T[]): T;
|
function min<T>(items: [T, ...T[]]): T;
|
||||||
|
function min(): undefined;
|
||||||
|
function min<T>(items?: T[]): T | undefined;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 파라미터
|
### 파라미터
|
||||||
|
@ -11,7 +11,9 @@ If the list is empty, returns `undefined`.
|
|||||||
## Signature
|
## Signature
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function max<T>(items: T[]): T;
|
function max<T>(items: [T, ...T[]]): T;
|
||||||
|
function max(): undefined;
|
||||||
|
function max<T>(items?: T[]): T | undefined;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Parameters
|
### Parameters
|
||||||
|
@ -11,7 +11,9 @@ If the list is empty, returns `undefined`.
|
|||||||
## Signature
|
## Signature
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function min<T>(items: T[]): T;
|
function min<T>(items: [T, ...T[]]): T;
|
||||||
|
function min(): undefined;
|
||||||
|
function min<T>(items?: T[]): T | undefined;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Parameters
|
### Parameters
|
||||||
|
@ -11,7 +11,9 @@
|
|||||||
## 签名
|
## 签名
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function max<T>(items: T[]): T;
|
function max<T>(items: [T, ...T[]]): T;
|
||||||
|
function max(): undefined;
|
||||||
|
function max<T>(items?: T[]): T | undefined;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 参数
|
### 参数
|
||||||
|
@ -11,7 +11,9 @@
|
|||||||
## 签名
|
## 签名
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function min<T>(items: T[]): T;
|
function min<T>(items: [T, ...T[]]): T;
|
||||||
|
function min(): undefined;
|
||||||
|
function min<T>(items?: T[]): T | undefined;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 参数
|
### 参数
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* @template T - The type of elements in the array.
|
* @template T - The type of elements in the array.
|
||||||
* @param {T[]} items The array of elements to search.
|
* @param {T[]} items The array of elements to search.
|
||||||
* @returns {T} The element with the maximum value.
|
* @returns {T | undefined} The element with the maximum value.
|
||||||
* @example
|
* @example
|
||||||
* // Returns 9
|
* // Returns 9
|
||||||
* max([3, 1, 4, 1, 5, 9])
|
* max([3, 1, 4, 1, 5, 9])
|
||||||
@ -12,7 +12,10 @@
|
|||||||
* // Returns 8
|
* // Returns 8
|
||||||
* max([0, -3, 2, 8, 7])
|
* max([0, -3, 2, 8, 7])
|
||||||
*/
|
*/
|
||||||
export function max<T>(items: readonly T[] = []): T {
|
export function max<T>(items: readonly [T, ...T[]]): T;
|
||||||
|
export function max(): undefined;
|
||||||
|
export function max<T>(items?: readonly T[]): T | undefined;
|
||||||
|
export function max<T>(items: readonly T[] = []): T | undefined {
|
||||||
let maxElement = items[0];
|
let maxElement = items[0];
|
||||||
let max = undefined;
|
let max = undefined;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* @template T - The type of elements in the array.
|
* @template T - The type of elements in the array.
|
||||||
* @param {T[]} items The array of elements to search.
|
* @param {T[]} items The array of elements to search.
|
||||||
* @returns {T} The element with the minimum value.
|
* @returns {T | undefined} The element with the minimum value.
|
||||||
* @example
|
* @example
|
||||||
* // Returns 1
|
* // Returns 1
|
||||||
* min([3, 1, 4, 1, 5, 9])
|
* min([3, 1, 4, 1, 5, 9])
|
||||||
@ -12,6 +12,9 @@
|
|||||||
* // Returns -3
|
* // Returns -3
|
||||||
* min([0, -3, 2, 8, 7])
|
* min([0, -3, 2, 8, 7])
|
||||||
*/
|
*/
|
||||||
|
export function min<T>(items: readonly [T, ...T[]]): T;
|
||||||
|
export function min(): undefined;
|
||||||
|
export function min<T>(items?: readonly T[]): T | undefined;
|
||||||
export function min<T>(items: readonly T[] = []): T {
|
export function min<T>(items: readonly T[] = []): T {
|
||||||
let minElement = items[0];
|
let minElement = items[0];
|
||||||
let min = undefined;
|
let min = undefined;
|
||||||
|
Loading…
Reference in New Issue
Block a user