Skip to content

Commit

Permalink
🐛 Fix return type what optimize type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 16, 2021
1 parent 11270ab commit c3cf1f2
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/isBigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type IsBigint<T extends unknown> = T extends bigint ? true : false
* isBigint(1n) // true
* isBigint(1000) // false
*/
const isBigint = <T extends unknown>(val: T): IsBigint<T> =>
(typeof val === BIGINT) as IsBigint<T>
const isBigint = (val: unknown): val is bigint => typeof val === BIGINT

export { isBigint }
export type { IsBigint }
3 changes: 1 addition & 2 deletions src/isBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type IsBoolean<T extends unknown> = T extends boolean ? true : false
* isBoolean(true) // true
* isBoolean('hello') // false
*/
const isBoolean = <T extends unknown>(val: T): IsBoolean<T> =>
(typeof val === BOOLEAN) as IsBoolean<T>
const isBoolean = (val: unknown): val is boolean => typeof val === BOOLEAN

export { isBoolean }
export type { IsBoolean }
3 changes: 1 addition & 2 deletions src/isFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ type IsFunction<T extends unknown> = T extends AnyFn ? true : false
* isFunction(function) // true
* isFunction('hello') // false
*/
const isFunction = <T extends unknown>(val: T): IsFunction<T> =>
(typeof val === FUNCTION) as IsFunction<T>
const isFunction = (val: unknown): val is AnyFn => typeof val === FUNCTION

export { isFunction }
export type { IsFunction }
4 changes: 2 additions & 2 deletions src/isNill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type IsNill<T extends unknown> = IsUndefined<T> extends true
* isNumber(0) // true
* isNumber('hello') // false
*/
const isNill = <T extends unknown>(val: T): IsNill<T> =>
((isUndefined(val) || isNull(val)) as unknown) as IsNill<T>
const isNill = (val: unknown): val is null | undefined =>
isUndefined(val) || isNull(val)

export { isNill }
export type { IsNill }
3 changes: 1 addition & 2 deletions src/isNull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type IsNull<T extends unknown> = T extends null ? true : false
* isNull(null) // true
* isNull(undefined) // false
*/
const isNull = <T extends unknown>(val: T): IsNull<T> =>
(val === NULL) as IsNull<T>
const isNull = (val: unknown): val is null => val === NULL

export { isNull }
export type { IsNull }
3 changes: 1 addition & 2 deletions src/isNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type IsNumber<T extends unknown> = T extends number ? true : false
* isNumber(0) // true
* isNumber('hello') // false
*/
const isNumber = <T extends unknown>(val: T): IsNumber<T> =>
(typeof val === NUMBER) as IsNumber<T>
const isNumber = (val: unknown): val is number => typeof val === NUMBER

export { isNumber }
export type { IsNumber }
2 changes: 1 addition & 1 deletion src/isObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type IsObject<T extends unknown> = IsPrimitive<T> extends true ? false : true
* isObject([]) // true
* isObject('hello') // false
*/
const isObject = <T extends unknown>(val: unknown): IsObject<T> =>
const isObject = <T extends unknown>(val: T): val is T =>
!isPrimitive(val) as IsObject<T>

export { isObject }
Expand Down
5 changes: 3 additions & 2 deletions src/isPrimitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IsNill, isNill } from '@/isNill'
import { IsNumber, isNumber } from '@/isNumber'
import { IsString, isString } from '@/isString'
import { IsSymbol, isSymbol } from '@/isSymbol'
import { Primitive } from '@/types'

/**
* Primitive or not
Expand Down Expand Up @@ -44,10 +45,10 @@ type IsPrimitive<T extends unknown> = IsBigint<T> extends true
* isPrimitive(true) // true
* isPrimitive([]) // false
*/
const isPrimitive = <T extends unknown>(val: T): IsPrimitive<T> =>
const isPrimitive = (val: unknown): val is Primitive =>
[isNill, isBoolean, isNumber, isString, isBigint, isSymbol].some((is) =>
is(val)
) as IsPrimitive<T>
)

export { isPrimitive }
export type { IsPrimitive }
3 changes: 1 addition & 2 deletions src/isString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type IsString<T extends unknown> = T extends string ? true : false
* isString('hello world') // true
* isString(1000) // false
*/
const isString = <T extends unknown>(val: T): IsString<T> =>
(typeof val === STRING) as IsString<T>
const isString = (val: unknown): val is string => typeof val === STRING

export { isString }
export type { IsString }
3 changes: 1 addition & 2 deletions src/isSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type IsSymbol<T extends unknown> = T extends symbol ? true : false
* isSymbol(Symbol('hello')) // true
* isSymbol('hello') // false
*/
const isSymbol = <T extends unknown>(val: T): IsSymbol<T> =>
(typeof val === SYMBOL) as IsSymbol<T>
const isSymbol = (val: unknown): val is symbol => typeof val === SYMBOL

export { isSymbol }
export type { IsSymbol }
3 changes: 1 addition & 2 deletions src/isUndefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type IsUndefined<T extends unknown> = T extends undefined ? true : false
* isUndefined(undefined) // true
* isUndefined('hello') // false
*/
const isUndefined = <T extends unknown>(val: T): IsUndefined<T> =>
(typeof val === UNDEFINED) as IsUndefined<T>
const isUndefined = (val: unknown): val is undefined => typeof val === UNDEFINED

export { isUndefined }
export type { IsUndefined }
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
* @typeParam T - argument types
*/
export type AnyFn<T = any> = (...args: T[]) => unknown
export type Primitive =
| string
| number
| bigint
| boolean
| symbol
| undefined
| null

0 comments on commit c3cf1f2

Please sign in to comment.