Skip to content

Commit

Permalink
refactor: 타입 가드 함수의 추론 결과 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
rojiwon123 committed Oct 8, 2024
1 parent e28b55e commit 62da6fe
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 40 deletions.
4 changes: 1 addition & 3 deletions src/Lazy/dropRight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import concurrent, { isConcurrent } from "./concurrent";
function* sync<T>(length: number, iterable: Iterable<T>) {
const arr =
isArray(iterable) || isString(iterable) ? iterable : toArray(iterable);
for (let i = 0; i < arr.length - length; i++) {
yield arr[i];
}
for (let i = 0; i < arr.length - length; i++) yield arr[i] as T;
}

async function* asyncSequential<T>(length: number, iterable: AsyncIterable<T>) {
Expand Down
7 changes: 3 additions & 4 deletions src/Lazy/takeRight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ function* sync<A>(length: number, iterable: Iterable<A>): IterableIterator<A> {
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];
isIterable(iterable);
if (arr[i]) yield arr[i] as A;
}
}

async function* asyncSequential<T>(length: number, iterable: AsyncIterable<T>) {
const arr = await 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];
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/isArray.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type Include from "./types/Include";

/**
* Returns true if `a` is an Array.
*
Expand All @@ -9,7 +7,7 @@ import type Include from "./types/Include";
* isArray(2); // false
* ```
*/
const isArray = <T>(a: T): a is Include<T, unknown[] | Readonly<unknown[]>> =>
Array.isArray(a);
const isArray = (input: unknown): input is unknown[] | Readonly<unknown[]> =>
Array.isArray(input);

export default isArray;
6 changes: 2 additions & 4 deletions src/isBoolean.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type Include from "./types/Include";

/**
* Returns true if `n` is a Boolean.
*
Expand All @@ -10,6 +8,6 @@ import type Include from "./types/Include";
* isBoolean("FxTS"); // false
* ```
*/
const isBoolean = <T>(n: T): n is Include<T, boolean> => typeof n === "boolean";

const isBoolean = (input: unknown): input is boolean =>
typeof input === "boolean";
export default isBoolean;
6 changes: 2 additions & 4 deletions src/isNil.ts
Original file line number Diff line number Diff line change
@@ -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`.
Expand All @@ -13,7 +12,6 @@ import type Include from "./types/Include";
* isNil(null); // true
* ```
*/
const isNil = <T>(a: T): a is Include<T, null | undefined> =>
isUndefined(a) || isNull(a);

const isNil = (input: unknown): input is null | undefined =>
isUndefined(input) || isNull(input);
export default isNil;
4 changes: 1 addition & 3 deletions src/isNull.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type Include from "./types/Include";

/**
* Checks if the given value is `null`.
*
Expand All @@ -11,6 +9,6 @@ import type Include from "./types/Include";
* isNull(null); // true
* ```
*/
const isNull = <T>(input: T): input is Include<T, null> => input === null;
const isNull = (input: unknown): input is null => input === null;

export default isNull;
5 changes: 1 addition & 4 deletions src/isNumber.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type Include from "./types/Include";

/**
* Returns true if `n` is a Number.
*
Expand All @@ -9,6 +7,5 @@ import type Include from "./types/Include";
* isNumber("a"); // false
* ```
*/
const isNumber = <T>(n: T): n is Include<T, number> => typeof n === "number";

const isNumber = (input: unknown): input is number => typeof input === "number";
export default isNumber;
8 changes: 4 additions & 4 deletions src/isObject.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type Include from "./types/Include";
import isNull from "./isNull";

/**
* Checks if value is the type of object.
Expand All @@ -12,9 +12,9 @@ import type Include from "./types/Include";
* isObject(123); // false
* ```
*/
const isObject = <T>(a: T): a is Include<T, object> => {
const type = typeof a;
return a != null && (type === "object" || type === "function");
const isObject = (input: unknown): input is object => {
const type = typeof input;
return !isNull(input) && (type === "object" || type === "function");
};

export default isObject;
5 changes: 1 addition & 4 deletions src/isString.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type Include from "./types/Include";

/**
* Returns true if `s` is a String.
*
Expand All @@ -9,6 +7,5 @@ import type Include from "./types/Include";
* isString(2); // false
* ```
*/
const isString = <T>(s: T): s is Include<T, string> => typeof s === "string";

const isString = (input: unknown): input is string => typeof input === "string";
export default isString;
4 changes: 1 addition & 3 deletions src/isUndefined.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type Include from "./types/Include";

/**
* Checks if the given value is `undefined`.
*
Expand All @@ -9,6 +7,6 @@ import type Include from "./types/Include";
* isUndefined(2); // false
* ```
*/
const isUndefined = <T>(a: T): a is Include<T, undefined> => a === undefined;
const isUndefined = (input: unknown): input is undefined => input === undefined;

export default isUndefined;
3 changes: 0 additions & 3 deletions src/types/Include.ts

This file was deleted.

0 comments on commit 62da6fe

Please sign in to comment.