Ionic3,打字稿 - 忽略打字稿错误"属性在类型的值上不存在"



我已经浏览了这篇文章(以及这篇文章),以找到解决我问题的解决方案。

我想忽略这些错误:

Property 'payload' does not exist on type 'Matatu'.

Property 'key' does not exist on type 'Matatu'.

我已经尝试将变量声明"覆盖"类型系统,但它没有起作用。

这是我的.ts代码,到目前为止我尝试的内容:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ActionSheetController } from 'ionic-angular';
import { AngularFireList, AngularFireDatabase } from 'angularfire2/database';
import { Matatu } from '../../models/matatu/matatu.interface';
/* imports */
declare var payload;
declare var key;
@IonicPage()
@Component({
  selector: 'page-owner-dash',
  templateUrl: 'owner-dash.html',
})
export class OwnerDashPage {
  matatuListRef$: AngularFireList<Matatu>;
  matatuListAsync$: any;
  constructor(public navCtrl: NavController, public navParams: NavParams, private database: AngularFireDatabase, public actionShtCrtl: ActionSheetController) {
    this.matatuListRef$ = this.database.list('matatu-list');
    this.matatuListAsync$ = this.matatuListRef$.snapshotChanges();
  }
  ionViewDidLoad() {
   //stuff 
  }
  selectMatatu(matatu: Matatu){
    this.actionShtCrtl.create({
      title: `${matatu.payload.val().matNumberPlate}`,
      buttons: [
        {
          text: 'Edit',
          handler: () => {
            //stuff
          }          
        },
        {
          text: 'Delete',
          role: 'destructive',
          handler: () => {
            this.matatuListRef$.remove(matatu.key);
          }
        },
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            //do something
          }
        }
      ]
    }).present();
  }

}

这是Matatu结构:

export interface Matatu {
    $key?: string;
    matNumberPlate: string;
    matSacco: string;
    matDriver: string;
    matAccessCode: string;
    matStatus: string;
    matTracker: string;
    matLocation: string;
}

我如何将属性投入任何类型?

我如何将属性投入任何类型?

如果您只想施放它以避免打字稿错误,则可以做这样的事情:

// ...
title: `${(<any>matatu).payload.val().matNumberPlate}`,
// ...

// ...
this.matatuListRef$.remove((<any>matatu).key);
// ...

这样,matatu将被施加到any,并且错误应该消失。


如果您可以修改Matatu接口,我建议您在此添加这两个属性:

export interface Matatu {
    $key?: string;
    matNumberPlate: string;
    matSacco: string;
    matDriver: string;
    matAccessCode: string;
    matStatus: string;
    matTracker: string;
    matLocation: string;
    // New properties are optional
    payload?: any;
    key?: any;
}

这样,您就不需要施放matatu属性,因为payloadkey都是Matatu类型的定义的一部分(并且均为any类型)。

相关内容

  • 没有找到相关文章

最新更新