代码之家  ›  专栏  ›  技术社区  ›  Vinz243

未知服务器上的Typescript属性类型保护

  •  0
  • Vinz243  · 技术社区  · 3 年前

    我正在尝试键入未知类型的守卫

    const foo  = (obj: unknown) => {
        if (typeof obj === 'object' && obj) {
            if ('foo' in obj && typeof obj.foo === 'string') {
                return obj.foo;
            }
        }
    };
    

    But I'm getting

    我还尝试了is表达式不起作用:

    const foo  = (obj: unknown): obj is { foo: 'string' } => {
        if (typeof obj === 'object' && obj) {
            if ('foo' in obj && typeof obj.foo === 'string') {
                return obj;
            }
        }
        throw new Error();
    };
    
    2 回复  |  直到 3 年前
        1
  •  1
  •   Cerbrus    3 年前

    type fooObj = object & { foo: unknown };
    const foo = (obj: unknown) => {
        if (typeof obj === 'object' && obj) {
            if ('foo' in obj && typeof (obj as fooObj).foo === 'string') {
                return (obj as fooObj).foo;
            }
        }
    };
    
        2
  •  0
  •   captain-yossarian from Ukraine    3 年前

    请考虑使用此助手:

    const hasProperty = <Obj, Prop extends string>(obj: Obj, prop: Prop)
      : obj is Obj & Record<Prop, unknown> =>
      Object.prototype.hasOwnProperty.call(obj, prop);
    

    就你而言。 in here here here

    工作解决方案:

    const hasProperty = <Obj, Prop extends string>(obj: Obj, prop: Prop)
      : obj is Obj & Record<Prop, unknown> =>
      Object.prototype.hasOwnProperty.call(obj, prop);
    
    const foo = (obj: unknown) => {
      if (typeof obj === 'object' && obj) {
        if (hasProperty(obj, 'foo') && typeof obj.foo === 'string') {
          return obj.foo;
        }
      }
    };
    

    Playground

    obj assert function :

    const hasProperty = <Obj, Prop extends string>(obj: Obj, prop: Prop)
      : obj is Obj & Record<Prop, unknown> =>
      Object.prototype.hasOwnProperty.call(obj, prop);
    
    function foo(obj: unknown): asserts obj is { foo: string } {
      const isValid =
        typeof obj === 'object' &&
        obj &&
        hasProperty(obj, 'foo') &&
        typeof obj.foo === 'string';
    
      if (!isValid) {
        throw new Error();
      }
    
    };
    
    declare var obj: unknown;
    
    foo(obj);
    
    obj.foo // ok
    
    

    Playground