@meld-ts/core
    Preparing search index...

    Function isInferObject

    • 检查 obj 是否为对象(非 null、非数组),结果为真时推导 obj 为 T

      isPlainObject 的区别:本函数排除 DateMapSetError 等内置类实例,只做最基础的 typeof === 'object' && !Array.isArray() 检查。 如需严格判断"纯对象",请用 isPlainObject

      泛型行为:

      • 不指定 T → 默认 Record<string, unknown>
      • 指定 T 但不传 guard → obj 直接推断为 T(运行时只做 typeof 检查)
      • 传入 guard → 在 typeof 检查通过后附加 guard 判定。guard 签名为 (it: T) => boolean, 但也可以传 TypeGuard 型函数((it: T) => it is T),TS 同样接受且行为一致

      Type Parameters

      • T = Record<string, unknown>

      Parameters

      • obj: unknown

        — 任意类型变量

      • Optionalguard: (it: T) => boolean

        — 可选的附加断言函数,签名 (it: T) => boolean

      Returns obj is T

      true 当且仅当 obj 是非 null 对象(非数组)且 guard(若提供)返回 true

      // 基础:指定 T 收窄
      type TestA = { name?: string };
      if (isInferObject<TestA>(obj)) {
      console.log(obj.name || 'noname'); // obj: TestA
      }

      // 带 guard 做附加验证
      type TestB = { name: string };
      if (isInferObject<TestB>(obj, it => typeof it.name === 'string')) {
      console.log(obj.name); // obj: TestB 且 name 是 string
      }

      // guard 可以是 TypeGuard,省略显式泛型
      type WithVersion = { version: number };
      const isWithVersion = (it: WithVersion): it is WithVersion =>
      typeof it.version === 'number';
      if (isInferObject(val, isWithVersion)) {
      val.version += 1; // val: WithVersion
      }