abiA
Deno3y ago
abi

Simple type narrowing/type guard/assertion (?)

Is there really no simpler way of doing this without
any
and without external deps?

export function isFooBarBaz(
  value: unknown,
): value is { Foo: { Bar: { Baz: string } } } {
  return typeof value === "object" &&
    value !== null &&
    "Foo" in value &&
    typeof value.Foo === "object" &&
    value.Foo !== null &&
    "Bar" in value.Foo &&
    typeof value.Foo.Bar === "object" &&
    value.Foo.Bar !== null &&
    "Baz" in value.Foo.Bar &&
    typeof value.Foo.Bar.Baz === "string";
}


(What's the exact name for what I'm doing here btw? I confuse the terms.)
Was this page helpful?