简体   繁体   English

“对象”类型上不存在属性

[英]Property does not exist on type 'object'

I am getting various different data structures from external sources.我从外部来源获取各种不同的数据结构。 In get() i am checking for undefined and existence of the property, but still getting compiler errors .get()中,我正在检查属性的undefinedexistence ,但仍然出现编译器错误

Is there a quick fix for that as we are just prototyping at the moment?由于我们目前只是在制作原型,是否有快速解决方法?

class MyCache {
  private map: Map<string, object>;

  constructor() {
    this.map = new Map<string, object>();
  }

  populateMap(uuid: string): boolean {
    // ... getting a JSON object from somewhere
    let externalData: object = {};
    if (uuid == 'abc...') {
      externalData = {"bar": "BAR", "session": uuid}
    } else if( uuid == "def...") {
      externalData = {"foo": "FOO", "session": uuid}
    // ... some more else ifs ...
    } else {
      externalData = {"baz": "BAZ", "session": uuid}
    }
    this.map.set(uuid, externalData);
    console.log(externalData);
    return true;
  }

  get(uuid: string) {
    let response = this.map.get(uuid) || {'session': null};
    if (response.hasOwnProperty('session')) {
      return response.session;
    }
    return '';
  }
}

Error:错误:

Property 'session' does not exist on type 'object'.

AFAIK, type object is too general. AFAIK,类型object太笼统了。 I think it is better to use Record<string, unknown> .我认为最好使用Record<string, unknown> Please see ts eslint rule :请参阅ts eslint 规则

Unsafe:不安全:

//bad
const lowerObj: object = {};

Safe:安全的:

// good
const lowerObj: Record<string, unknown> = {};

Using hasOwnProperty is ok, but using Object.prototype.hasOwnProperty.call(foo, "bar") is much better.使用hasOwnProperty没问题,但使用Object.prototype.hasOwnProperty.call(foo, "bar")会好得多。 See eslint rule .请参阅eslint 规则

Here you can find recent optimization of V8 engine regarding using hasOwnProperty .在这里你可以找到 V8 引擎最近关于使用hasOwnProperty的优化。

You can find more typings for hasOwnProperty in my article你可以在我的文章中找到hasOwnProperty的更多类型

Hence, this code should be ok:因此,这段代码应该没问题:

class MyCache {
    private map: Map<string, Record<string, unknown>>;

    constructor() {
        this.map = new Map<string, Record<string, unknown>>();
    }

    populateMap(uuid: string): boolean {
        // ... getting a JSON Record<string, unknown> from somewhere
        let externalData: Record<string, unknown> = {};
        if (uuid == 'abc...') {
            externalData = { "bar": "BAR", "session": uuid }
        } else if (uuid == "def...") {
            externalData = { "foo": "FOO", "session": uuid }
            // ... some more else ifs ...
        } else {
            externalData = { "baz": "BAZ", "session": uuid }
        }
        this.map.set(uuid, externalData);
        console.log(externalData);
        return true;
    }

    get(uuid: string) {
        let response = this.map.get(uuid) || { 'session': null };
        if (response.hasOwnProperty('session')) {
            return response.session;
        }
        return '';
    }
}

Playground 操场

  1. Use Object instead of object link使用Object而不是object 链接
  2. Use 'key' in obj instead of hasOwnProperty link 'key' in obj而不是hasOwnProperty 链接
class MyCache {
    private map: Map<string, Object>;

    constructor() {
        this.map = new Map(); // No need for generic parameters, type is already set in the property declaration
    }

    get(uuid: string) {
        let response = this.map.get(uuid) || { 'session': null };
        if ('session' in response) {
            return response.session;
        }
        return '';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM