chore: add basic type assertion util

This commit is contained in:
EnixCoda 2022-05-17 23:06:38 +08:00
parent 2c93bc64eb
commit 9c6ccbe2fc
2 changed files with 25 additions and 2 deletions

11
src/global.d.ts vendored
View file

@ -17,8 +17,8 @@ type TreeNode = {
sha?: string
accessDenied?: boolean
comments?: {
active: number,
resolved: number,
active: number
resolved: number
}
diff?: {
status: 'modified' | 'added' | 'removed' | 'renamed'
@ -43,3 +43,10 @@ type VoidFN<T> = (payload: T) => void
type Async<T> = T | Promise<T>
type EnumString<S extends string> = S | (string & {})
type JSONPrimitive = string | number | boolean | null | undefined
type JSONObject = {
[key: string]: JSONValue
}
type JSONArray = JSONValue[]
type JSONValue = JSONPrimitive | JSONObject | JSONArray

16
src/utils/is.ts Normal file
View file

@ -0,0 +1,16 @@
export const is = {
boolean: (d: unknown): d is boolean => typeof d === 'boolean',
number: (d: unknown): d is number => typeof d === 'number',
string: (d: unknown): d is string => typeof d === 'string',
JSON: {
object(d: unknown): d is JSONObject {
return (typeof d === 'object' && d && !Array.isArray(d)) || false
},
array(d: unknown): d is JSONArray {
return Array.isArray(d)
},
plain(d: unknown): d is JSONPrimitive {
return d === null || d === 'undefined' || is.boolean(d) || is.number(d) || is.string(d)
},
},
}