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,7 +11,9 @@
## 인터페이스
```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
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
```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

View File

@ -11,7 +11,9 @@ If the list is empty, returns `undefined`.
## Signature
```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

View File

@ -11,7 +11,9 @@
## 签名
```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
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.
* @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
* // Returns 9
* max([3, 1, 4, 1, 5, 9])
@ -12,7 +12,10 @@
* // Returns 8
* 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 max = undefined;

View File

@ -3,7 +3,7 @@
*
* @template T - The type of elements in the array.
* @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
* // Returns 1
* min([3, 1, 4, 1, 5, 9])
@ -12,6 +12,9 @@
* // Returns -3
* 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 {
let minElement = items[0];
let min = undefined;