Skip to content

Commit

Permalink
refactor: typeguard 타입추론 개선 (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
rojiwon123 authored Oct 17, 2024
1 parent 3eb22f5 commit 850820a
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Lazy/dropRight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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];
yield arr[i] as T;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lazy/takeRight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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];
if (arr[i]) yield arr[i] as A;
}
}

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 = <T>(input: T): input is T & (unknown[] | Readonly<unknown[]>) =>
Array.isArray(input);

export default isArray;
5 changes: 2 additions & 3 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,7 @@ import type Include from "./types/Include";
* isBoolean("FxTS"); // false
* ```
*/
const isBoolean = <T>(n: T): n is Include<T, boolean> => typeof n === "boolean";
const isBoolean = <T>(input: T): input is T & boolean =>
typeof input === "boolean";

export default isBoolean;
5 changes: 2 additions & 3 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,7 @@ 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 = <T>(input: T): input is T & (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 = <T>(input: T): input is T & null => input === null;

export default isNull;
5 changes: 2 additions & 3 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,7 @@ import type Include from "./types/Include";
* isNumber("a"); // false
* ```
*/
const isNumber = <T>(n: T): n is Include<T, number> => typeof n === "number";
const isNumber = <T>(input: T): input is T & 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 isNil from "./isNil";

/**
* 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 = <T>(input: T): input is T & object => {
const type = typeof input;
return !isNil(input) && (type === "object" || type === "function");
};

export default isObject;
5 changes: 2 additions & 3 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,7 @@ import type Include from "./types/Include";
* isString(2); // false
* ```
*/
const isString = <T>(s: T): s is Include<T, string> => typeof s === "string";
const isString = <T>(input: T): input is T & string =>
typeof input === "string";

export default isString;
5 changes: 2 additions & 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,7 @@ import type Include from "./types/Include";
* isUndefined(2); // false
* ```
*/
const isUndefined = <T>(a: T): a is Include<T, undefined> => a === undefined;
const isUndefined = <T>(input: T): input is T & undefined =>
input === undefined;

export default isUndefined;
5 changes: 5 additions & 0 deletions src/types/FalsyTypeOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type Falsy from "./Falsy";

type FalsyTypesOf<T> = T extends Falsy ? T : never;

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

This file was deleted.

6 changes: 3 additions & 3 deletions type-check/throwError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const res2 = pipe(
);

const res3 = pipe(
0,
0 as const,

unless(
isNumber,
Expand All @@ -31,6 +31,6 @@ const res4 = pipe(
checks([
check<typeof res1, never, Test.Pass>(),
check<typeof res2, never, Test.Pass>(),
check<typeof res3, number, Test.Pass>(),
check<typeof res4, number, Test.Pass>(),
check<typeof res3, 0, Test.Pass>(),
check<typeof res4, never, Test.Pass>(),
]);

0 comments on commit 850820a

Please sign in to comment.