Skip to content

Commit

Permalink
chore: 修改类型提示
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengxs2018 committed May 5, 2021
1 parent 5702c74 commit d6666c1
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions tools/lodash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ export function defaultTo<T>(value: T | undefined | null, defaultValue: T): T {
return isNil(value) ? defaultValue : value
}

/**
* 获取值
*
* like lodash/get
*
* @param object - 对象
* @param value - 未知数据
*/
export function get<T extends Record<string, unknown>, K extends keyof T>(
object: T,
path: K
): Exclude<T[K], undefined>

/**
* 获取值
*
* like lodash/get
*
* @param object - 对象
* @param value - 未知数据
* @param defaultValue - 默认值
*/
export function get<T extends Record<string, unknown>, K extends keyof T, D>(
object: T,
path: K,
defaultValue: D
): Exclude<T[K], undefined>

/**
* 获取值
*
Expand All @@ -30,10 +58,10 @@ export function defaultTo<T>(value: T | undefined | null, defaultValue: T): T {
* @param value - 未知数据
* @param defaultValue - 默认值
*/
export function get<T extends Record<string, D>, K extends keyof T, D>(
object: T | null | undefined,
export function get<T extends Record<string, unknown>, K extends keyof T, D>(
object: T,
path: K,
defaultValue?: D
): Exclude<T[K], undefined> | D {
return defaultTo(object[path], defaultValue)
): Exclude<T[K], undefined> | D | undefined {
return defaultTo(object[path] as D, defaultValue)
}

0 comments on commit d6666c1

Please sign in to comment.