0
0
mirror of https://github.com/renovatebot/renovate.git synced 2024-12-22 05:28:35 +00:00
renovatebot_renovate/lib/util/object.ts
2023-11-07 15:50:29 +00:00

23 lines
621 B
TypeScript

/**
* This is a workaround helper to allow the usage of 'unknown' in
* a type-guard function while checking that keys exist.
*
* @see https://github.com/microsoft/TypeScript/issues/21732
* @see https://stackoverflow.com/a/58630274
*/
export function hasKey<K extends string, T>(
k: K,
o: T,
): o is T & Record<K, unknown> {
return o && typeof o === 'object' && k in o;
}
/**
* Coerce a value to a object with optional default value.
* @param val value to coerce
* @returns the coerced value.
*/
export function coerceObject<T>(val: T | null | undefined, def?: T): T {
return val ?? def ?? ({} as T);
}