通过接口属性从对象获取价值?

时间:2018-09-02 13:57:19

标签: typescript typescript2.0

我有一个自定义类:

class CalendarThematicPlanModel extends FilterMethods<ICalendarThematicPlanShort> implements IModel<W> {
    public constructor(public model?: ICalendarThematicPlanShort) {
        super();
    }

    public validField<ICalendarThematicPlanShort, K extends keyof ICalendarThematicPlanShort>(key:K) { 
        this.model[key];
    }
}

我想确保方法validField(key)中传递的键在接口ICalendarThematicPlanShortthis.model中肯定存在。

1 个答案:

答案 0 :(得分:2)

问题是您定义了与接口名称相同的通用参数。因此,const rP = require('request-promise'); rP({ 'method' : 'GET', 'uri' : 'https://football-data.org/v2/competitions/PL/matches', 'json' : true, 'headers' : { 'X-Auth-Token' : 'api-key' }, 'insecure': true }) .then(console.log, console.log); 是该泛型类型参数的键,而不是您的接口的键:

RequestError: Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's altnames: Host: football-data.org. is not in the cert's altnames: DNS:api.football-data.org