From 11e94335af1681df8ce6a3f1a08da76bc2d91c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=85=B8=EC=A7=80=EC=9B=90?= <116284195+rojiwon123@users.noreply.github.com> Date: Tue, 8 Oct 2024 07:45:10 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20typeguard=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=EC=B6=94=EB=A1=A0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Lazy/dropRight.ts | 2 +- src/Lazy/takeRight.ts | 2 +- src/isArray.ts | 6 ++---- src/isBoolean.ts | 5 ++--- src/isNil.ts | 5 ++--- src/isNull.ts | 4 +--- src/isNumber.ts | 5 ++--- src/isObject.ts | 8 ++++---- src/isString.ts | 5 ++--- src/isUndefined.ts | 5 ++--- src/types/FalsyTypeOf.ts | 5 +++++ src/types/Include.ts | 3 --- type-check/throwError.test.ts | 6 +++--- 13 files changed, 27 insertions(+), 34 deletions(-) create mode 100644 src/types/FalsyTypeOf.ts delete mode 100644 src/types/Include.ts diff --git a/src/Lazy/dropRight.ts b/src/Lazy/dropRight.ts index 92d9695b..618af9a1 100644 --- a/src/Lazy/dropRight.ts +++ b/src/Lazy/dropRight.ts @@ -9,7 +9,7 @@ function* sync(length: number, iterable: Iterable) { const arr = isArray(iterable) || isString(iterable) ? iterable : toArray(iterable); for (let i = 0; i < arr.length - length; i++) { - yield arr[i]; + yield arr[i] as T; } } diff --git a/src/Lazy/takeRight.ts b/src/Lazy/takeRight.ts index a96d4bc2..0841275d 100644 --- a/src/Lazy/takeRight.ts +++ b/src/Lazy/takeRight.ts @@ -11,7 +11,7 @@ function* sync(length: number, iterable: Iterable): IterableIterator { isArray(iterable) || isString(iterable) ? iterable : toArray(iterable); const index = arr.length - length; for (let i = index; i < arr.length; i++) { - if (arr[i]) yield arr[i]; + if (arr[i]) yield arr[i] as A; } } diff --git a/src/isArray.ts b/src/isArray.ts index 67a34607..af728e4f 100644 --- a/src/isArray.ts +++ b/src/isArray.ts @@ -1,5 +1,3 @@ -import type Include from "./types/Include"; - /** * Returns true if `a` is an Array. * @@ -9,7 +7,7 @@ import type Include from "./types/Include"; * isArray(2); // false * ``` */ -const isArray = (a: T): a is Include> => - Array.isArray(a); +const isArray = (input: T): input is T & (unknown[] | Readonly) => + Array.isArray(input); export default isArray; diff --git a/src/isBoolean.ts b/src/isBoolean.ts index 4372fabc..ea46e7a6 100644 --- a/src/isBoolean.ts +++ b/src/isBoolean.ts @@ -1,5 +1,3 @@ -import type Include from "./types/Include"; - /** * Returns true if `n` is a Boolean. * @@ -10,6 +8,7 @@ import type Include from "./types/Include"; * isBoolean("FxTS"); // false * ``` */ -const isBoolean = (n: T): n is Include => typeof n === "boolean"; +const isBoolean = (input: T): input is T & boolean => + typeof input === "boolean"; export default isBoolean; diff --git a/src/isNil.ts b/src/isNil.ts index 05e0aa03..bb633a52 100644 --- a/src/isNil.ts +++ b/src/isNil.ts @@ -1,6 +1,5 @@ import isNull from "./isNull"; import isUndefined from "./isUndefined"; -import type Include from "./types/Include"; /** * Checks if the given value is `null` or `undefined`. @@ -13,7 +12,7 @@ import type Include from "./types/Include"; * isNil(null); // true * ``` */ -const isNil = (a: T): a is Include => - isUndefined(a) || isNull(a); +const isNil = (input: T): input is T & (null | undefined) => + isUndefined(input) || isNull(input); export default isNil; diff --git a/src/isNull.ts b/src/isNull.ts index 39dbc14e..1f1e1784 100644 --- a/src/isNull.ts +++ b/src/isNull.ts @@ -1,5 +1,3 @@ -import type Include from "./types/Include"; - /** * Checks if the given value is `null`. * @@ -11,6 +9,6 @@ import type Include from "./types/Include"; * isNull(null); // true * ``` */ -const isNull = (input: T): input is Include => input === null; +const isNull = (input: T): input is T & null => input === null; export default isNull; diff --git a/src/isNumber.ts b/src/isNumber.ts index 73ce9a93..947186d5 100644 --- a/src/isNumber.ts +++ b/src/isNumber.ts @@ -1,5 +1,3 @@ -import type Include from "./types/Include"; - /** * Returns true if `n` is a Number. * @@ -9,6 +7,7 @@ import type Include from "./types/Include"; * isNumber("a"); // false * ``` */ -const isNumber = (n: T): n is Include => typeof n === "number"; +const isNumber = (input: T): input is T & number => + typeof input === "number"; export default isNumber; diff --git a/src/isObject.ts b/src/isObject.ts index 3b5f0aed..b2b2252b 100644 --- a/src/isObject.ts +++ b/src/isObject.ts @@ -1,4 +1,4 @@ -import type Include from "./types/Include"; +import isNil from "./isNil"; /** * Checks if value is the type of object. @@ -12,9 +12,9 @@ import type Include from "./types/Include"; * isObject(123); // false * ``` */ -const isObject = (a: T): a is Include => { - const type = typeof a; - return a != null && (type === "object" || type === "function"); +const isObject = (input: T): input is T & object => { + const type = typeof input; + return !isNil(input) && (type === "object" || type === "function"); }; export default isObject; diff --git a/src/isString.ts b/src/isString.ts index 90362359..942d10ab 100644 --- a/src/isString.ts +++ b/src/isString.ts @@ -1,5 +1,3 @@ -import type Include from "./types/Include"; - /** * Returns true if `s` is a String. * @@ -9,6 +7,7 @@ import type Include from "./types/Include"; * isString(2); // false * ``` */ -const isString = (s: T): s is Include => typeof s === "string"; +const isString = (input: T): input is T & string => + typeof input === "string"; export default isString; diff --git a/src/isUndefined.ts b/src/isUndefined.ts index 2a3d63eb..e5d67ed6 100644 --- a/src/isUndefined.ts +++ b/src/isUndefined.ts @@ -1,5 +1,3 @@ -import type Include from "./types/Include"; - /** * Checks if the given value is `undefined`. * @@ -9,6 +7,7 @@ import type Include from "./types/Include"; * isUndefined(2); // false * ``` */ -const isUndefined = (a: T): a is Include => a === undefined; +const isUndefined = (input: T): input is T & undefined => + input === undefined; export default isUndefined; diff --git a/src/types/FalsyTypeOf.ts b/src/types/FalsyTypeOf.ts new file mode 100644 index 00000000..e8082a73 --- /dev/null +++ b/src/types/FalsyTypeOf.ts @@ -0,0 +1,5 @@ +import type Falsy from "./Falsy"; + +type FalsyTypesOf = T extends Falsy ? T : never; + +export default FalsyTypesOf; diff --git a/src/types/Include.ts b/src/types/Include.ts deleted file mode 100644 index bc9c23d8..00000000 --- a/src/types/Include.ts +++ /dev/null @@ -1,3 +0,0 @@ -type Include = T extends N ? T : never; - -export default Include; diff --git a/type-check/throwError.test.ts b/type-check/throwError.test.ts index ddd5dfda..e29499b6 100644 --- a/type-check/throwError.test.ts +++ b/type-check/throwError.test.ts @@ -11,7 +11,7 @@ const res2 = pipe( ); const res3 = pipe( - 0, + 0 as const, unless( isNumber, @@ -31,6 +31,6 @@ const res4 = pipe( checks([ check(), check(), - check(), - check(), + check(), + check(), ]);