Skip to content

Commit c117ff8

Browse files
authored
refactor: enhance typeguard function type (#211)
* refactor: convert to generic function * refactor: enhance isArray
1 parent 3f19039 commit c117ff8

20 files changed

+138
-36
lines changed

src/isArray.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type Include from "./types/Include";
2+
13
/**
24
* Returns true if `a` is an Array.
35
*
@@ -7,8 +9,7 @@
79
* isArray(2); // false
810
* ```
911
*/
10-
function isArray(a: any): a is any[] {
11-
return Array.isArray(a);
12-
}
12+
const isArray = <T>(a: T): a is Include<T, unknown[] | Readonly<unknown[]>> =>
13+
Array.isArray(a);
1314

1415
export default isArray;

src/isBoolean.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type Include from "./types/Include";
2+
13
/**
24
* Returns true if `n` is a Boolean.
35
*
@@ -8,8 +10,6 @@
810
* isBoolean("FxTS"); // false
911
* ```
1012
*/
11-
function isBoolean(n: unknown): n is boolean {
12-
return typeof n === "boolean";
13-
}
13+
const isBoolean = <T>(n: T): n is Include<T, boolean> => typeof n === "boolean";
1414

1515
export default isBoolean;

src/isEmpty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import isNil from "./isNil";
1919
* isEmpty(new Date()) // false
2020
* ```
2121
*/
22-
export const isEmpty = (value: unknown): boolean => {
22+
const isEmpty = <T>(value: T): boolean => {
2323
if (isNil(value)) return true; // if value is null or undefined.
2424

2525
if (

src/isNil.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import isNull from "./isNull";
22
import isUndefined from "./isUndefined";
3-
4-
type Nullable<T> = T extends null | undefined ? T : never;
3+
import type Include from "./types/Include";
54

65
/**
76
* Checks if the given value is `null` or `undefined`.
@@ -14,6 +13,7 @@ type Nullable<T> = T extends null | undefined ? T : never;
1413
* isNil(null); // true
1514
* ```
1615
*/
17-
const isNil = <T>(a: T): a is Nullable<T> => isUndefined(a) || isNull(a);
16+
const isNil = <T>(a: T): a is Include<T, null | undefined> =>
17+
isUndefined(a) || isNull(a);
1818

1919
export default isNil;

src/isNull.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type Include from "./types/Include";
2+
13
/**
24
* Checks if the given value is `null`.
35
*
@@ -9,6 +11,6 @@
911
* isNull(null); // true
1012
* ```
1113
*/
12-
const isNull = <T>(input: T | null): input is null => input === null;
14+
const isNull = <T>(input: T): input is Include<T, null> => input === null;
1315

1416
export default isNull;

src/isNumber.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type Include from "./types/Include";
2+
13
/**
24
* Returns true if `n` is a Number.
35
*
@@ -7,8 +9,6 @@
79
* isNumber("a"); // false
810
* ```
911
*/
10-
function isNumber(n: unknown): n is number {
11-
return typeof n === "number";
12-
}
12+
const isNumber = <T>(n: T): n is Include<T, number> => typeof n === "number";
1313

1414
export default isNumber;

src/isObject.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type IdentityObject<T> = T extends object ? T : never;
1+
import type Include from "./types/Include";
22

33
/**
44
* Checks if value is the type of object.
@@ -12,9 +12,9 @@ type IdentityObject<T> = T extends object ? T : never;
1212
* isObject(123); // false
1313
* ```
1414
*/
15-
function isObject<T = unknown>(a: T): a is IdentityObject<T> {
15+
const isObject = <T>(a: T): a is Include<T, object> => {
1616
const type = typeof a;
1717
return a != null && (type === "object" || type === "function");
18-
}
18+
};
1919

2020
export default isObject;

src/isString.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type Include from "./types/Include";
2+
13
/**
24
* Returns true if `s` is a String.
35
*
@@ -7,8 +9,6 @@
79
* isString(2); // false
810
* ```
911
*/
10-
function isString(s: unknown): s is string {
11-
return typeof s === "string";
12-
}
12+
const isString = <T>(s: T): s is Include<T, string> => typeof s === "string";
1313

1414
export default isString;

src/isUndefined.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type Include from "./types/Include";
2+
13
/**
24
* Checks if the given value is `undefined`.
35
*
@@ -7,6 +9,6 @@
79
* isUndefined(2); // false
810
* ```
911
*/
10-
const isUndefined = <T>(a: T | undefined): a is undefined => a === undefined;
12+
const isUndefined = <T>(a: T): a is Include<T, undefined> => a === undefined;
1113

1214
export default isUndefined;

src/last.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function last<T extends Iterable<unknown> | AsyncIterable<unknown>>(
4747
iterable: T,
4848
): LastReturnType<T>;
4949

50-
function last<A>(iterable: Iterable<A> | AsyncIterable<A>) {
50+
function last<A>(iterable: Iterable<A> | AsyncIterable<A> | A[]) {
5151
if (isArray(iterable) || isString(iterable)) {
5252
return iterable[iterable.length - 1];
5353
}

0 commit comments

Comments
 (0)