【问题标题】:Ionic3, Typescript - Ignore Typescript Errors “property ' ' does not exist on value of type ' '”Ionic3,打字稿 - 忽略打字稿错误“属性''不存在于类型''的值”
【发布时间】:2018-05-16 23:01:34
【问题描述】:

我已经通过this post(以及this one)找到了我的问题的解决方案。

我想忽略这些错误:

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;

}

如何将属性转换为任何类型?

【问题讨论】:

  • 能否再添加Matatu类型的定义?
  • 这是一个界面。就是这样。

标签: angular typescript ionic2 ionic3


【解决方案1】:

如何将属性转换为任何类型?

如果你只是想强制转换以避免打字稿错误,你可以这样做:

// ...
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 类型)。

【讨论】:

    猜你喜欢
    • 2021-02-19
    • 2017-03-26
    • 1970-01-01
    • 2018-11-09
    • 2017-03-02
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多