refactor: improve type definitions and clean up type assertions inafter, isKey, orderBy, and withTimeout functions (#703)

* Fix after

* Remove type assertion
This commit is contained in:
Dayong Lee 2024-10-13 22:04:01 +09:00 committed by GitHub
parent eb18184691
commit 59e973708f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 30 additions and 15 deletions

View File

@ -8,7 +8,10 @@
## インターフェース
```typescript
function after<F extends (...args: any[]) => any>(n: number, func: F): F;
function after<F extends (...args: any[]) => any>(
n: number,
func: F
): (...args: Parameters<F>) => ReturnType<F> | undefined;
```
### パラメータ
@ -18,7 +21,7 @@ function after<F extends (...args: any[]) => any>(n: number, func: F): F;
### 戻り値
(`F`): 新しい関数を返します。この関数は以下の機能を持ちます:
(`(...args: Parameters<F>) => ReturnType<F> | undefined`): 新しい関数を返します。この関数は以下の機能を持ちます:
- 呼び出された回数を追跡します。
- `n`回目の呼び出しから`func`を呼び出します。

View File

@ -8,7 +8,10 @@
## 인터페이스
```typescript
function after<F extends (...args: any[]) => any>(n: number, func: F): F;
function after<F extends (...args: any[]) => any>(
n: number,
func: F
): (...args: Parameters<F>) => ReturnType<F> | undefined;
```
### 파라미터
@ -18,7 +21,7 @@ function after<F extends (...args: any[]) => any>(n: number, func: F): F;
### 반환 값
(`F`): 새로운 함수를 반환해요. 이 함수는 다음과 같은 기능을 가져요.
(`(...args: Parameters<F>) => ReturnType<F> | undefined`): 새로운 함수를 반환해요. 이 함수는 다음과 같은 기능을 가져요.
- 호출된 횟수를 추적해요.
- `n`번째 호출부터 `func`을 호출해요.

View File

@ -8,7 +8,10 @@ The is particularly useful for scenarios involving events or asynchronous operat
## Signature
```typescript
function after<F extends (...args: any[]) => any>(n: number, func: F): F;
function after<F extends (...args: any[]) => any>(
n: number,
func: F
): (...args: Parameters<F>) => ReturnType<F> | undefined;
```
### Parameters
@ -18,7 +21,7 @@ function after<F extends (...args: any[]) => any>(n: number, func: F): F;
### Returns
(`F`): A new function that:
(`(...args: Parameters<F>) => ReturnType<F> | undefined`): A new function that:
- Tracks the number of calls.
- Invokes `func` starting from the `n`-th call.

View File

@ -8,7 +8,10 @@
## 签名
```typescript
function after<F extends (...args: any[]) => any>(n: number, func: F): F;
function after<F extends (...args: any[]) => any>(
n: number,
func: F
): (...args: Parameters<F>) => ReturnType<F> | undefined;
```
### 参数
@ -18,7 +21,7 @@ function after<F extends (...args: any[]) => any>(n: number, func: F): F;
### 返回值
(`F`): 一个新函数,它:
(`(...args: Parameters<F>) => ReturnType<F> | undefined`): 一个新函数,它:
- 跟踪调用次数。
- 从第 `n` 次调用开始调用 `func`

View File

@ -18,7 +18,7 @@ const regexIsPlainProp = /^\w*$/;
* isKey('a.b', { a: { b: 2 } });
* // => false
*/
export function isKey(value?: unknown, object?: unknown): boolean {
export function isKey(value?: unknown, object?: unknown): value is PropertyKey {
if (Array.isArray(value)) {
return false;
}

View File

@ -107,7 +107,7 @@ export function orderBy<T>(
}
// If criterion is not key, it has possibility to be a deep path. So we have to prepare both cases.
return { key: criterion, path: toPath(criterion as string) } as const;
return { key: criterion, path: toPath(criterion) };
});
// Array.prototype.sort() always shifts the `undefined` values to the end of the array. So we have to prevent it by using a wrapper object.

View File

@ -8,7 +8,7 @@
* @template F - The type of the function to be invoked.
* @param {number} n - The number of calls required for `func` to execute.
* @param {F} func - The function to be invoked.
* @returns {F} - A new function that:
* @returns {(...args: Parameters<F>) => ReturnType<F> | undefined} - A new function that:
* - Tracks the number of calls.
* - Invokes `func` starting from the `n`-th call.
* - Returns `undefined` if fewer than `n` calls have been made.
@ -27,16 +27,19 @@
* afterFn()
*/
export function after<F extends (...args: any[]) => any>(n: number, func: F): F {
export function after<F extends (...args: any[]) => any>(
n: number,
func: F
): (...args: Parameters<F>) => ReturnType<F> | undefined {
if (!Number.isInteger(n) || n < 0) {
throw new Error(`n must be a non-negative integer.`);
}
let counter = 0;
return ((...args: Parameters<F>) => {
return (...args: Parameters<F>) => {
if (++counter >= n) {
return func(...args);
}
return undefined;
}) as F;
};
}

View File

@ -26,5 +26,5 @@ import { timeout } from './timeout.ts';
* }
*/
export async function withTimeout<T>(run: () => Promise<T>, ms: number): Promise<T> {
return Promise.race([run(), timeout(ms) as T]);
return Promise.race([run(), timeout(ms)]);
}