Functionutilsv0.10.0
hasKeys<Obj, Keys>(
obj: Obj,
keys: Keys[]
): obj is Obj &
UnionToIntersection<
Keys extends unknown
? PathToObj<
Keys,
UnionToIntersection<
NonNullable<DeepGet<Obj, Keys>>
>
>
: never
>interface Test {
a?: {
b?: {
c?: string;
};
} | null;
x?: number | null;
}
const obj: Test = { a: { b: { c: 'hello' } }, x: 42 };
if (hasKeys(obj, ['a.b.c', 'x'])) {
// Here, obj is narrowed to:
// {
// a: { b: { c: string } };
// x: number;
// }
console.log(obj.a.b.c.toUpperCase());
console.log(obj.x.toFixed(2));
}Obj
ObjKeys
Keysobj
obj: Objkeys
keys: Keys[]An array of dot-notation paths to check.