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:
D-Sketon 2024-07-21 16:49:43 +08:00 committed by GitHub
parent 999a4daf87
commit d117481dc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 29 additions and 11 deletions

View File

@ -11,14 +11,16 @@
## 인터페이스 ## 인터페이스
```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;
``` ```
### 파라미터 ### 파라미터
- `items` (`T[]`): 최댓값을 가지는 요소를 찾을 배열. - `items` (`T[]`): 최댓값을 가지는 요소를 찾을 배열.
### 반환 값 ### 반환 값
(`T`): 배열에서 최솟값을 가지는 요소. 배열이 비어 있다면 `undefined`를 반환해요. (`T`): 배열에서 최솟값을 가지는 요소. 배열이 비어 있다면 `undefined`를 반환해요.

View File

@ -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;
``` ```
### 파라미터 ### 파라미터

View File

@ -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

View File

@ -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

View File

@ -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;
``` ```
### 参数 ### 参数

View File

@ -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;
``` ```
### 参数 ### 参数

View File

@ -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;

View File

@ -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;
@ -24,4 +27,4 @@ export function min<T>(items: readonly T[] = []): T {
} }
return minElement; return minElement;
} }