打字稿:“X”不能分配给类型,属性“Y”的类型是不兼容的

时间:2017-07-07 21:47:34

标签: angular date typescript

注意:我对Angular2和打字稿很新,所以我很抱歉我的无知。

问题: 我要做的是从结束日期/时间中减去开始日期/时间,然后在计算公式中使用它来显示为“calc”。问题是我可以给calc一个有效的静态数字,但如果我尝试使用数学公式,它会在转换时出现各种错误:

  

1   mock-orders.ts(4,14):错误TS2322:输入'{order_no:string;预定的:字符串;侧面:弦; start_time:string;检查:字符串; stop _...'不能分配给'Order []'。

     

输入'{order_no:string;预定的:字符串;侧面:弦; start_time:string;检查:字符串; stop _...'不能指定为'Order'类型。

     

属性'检查'的类型不兼容。

     

类型'string'不能分配给'number'类型。

以下是我遇到这些问题的子组件。 见第28行this.calc:

     // order.ts
import { Component, OnInit } from '@angular/core';

export class Order {
    order_no: string;
    scheduled: string;
    lateral: string;
    start_time: string;
    checks: number;
    stop_time: string;
    status: string;
    approx_cfs: string;
    approx_hrs: string;
    approx_af: string;
    calc: number;

    constructor(data: {} = {}) {
    this.order_no = data["order_no"] || "";
    this.scheduled = data["scheduled"] || "";
    this.lateral = data["lateral"] || "";
    this.start_time = data["start_time"] || "";
    this.checks = data["checks"] || "";
    this.stop_time = data["stop_time"] || "";
    this.status = data["status"] || "";
    this.approx_cfs = data["approx_cfs"] || "";
    this.approx_hrs = data["approx_hrs"] || "";
    this.approx_af = data["approx_af"] || "";
    this.calc = (!this.stop_time ? ((new Date().getTime() - new Date(this.start_time).getTime()) / 1000.0 / 60.0 / 60.0) * this.checks * 0.0825 : ((new Date(this.stop_time).getTime() - new Date(this.start_time).getTime()) /1000.0 / 60.0 / 60.0) * this.checks * 0.0825); 

    console.log(this.calc);
    };

};

问题的一部分,我确定,我正在尝试在日期上使用数学运算,然后将结果分配给数字类型。

以下调用此组件以在服务中使用:

// order.service.ts
import { Injectable } from '@angular/core';

import { Order } from './order';
import { ORDERS } from './mock-orders';

@Injectable()
export class OrderService {
  getOrders(): Promise<Order[]> {
    return Promise.resolve(ORDERS);
  }
}

以下文件是每次运行服务时提取的数据数组,用于描述将从数据库接收的内容。

// mock-orders.ts
import { Order } from './order'

export const ORDERS: Order[] = [ 
    {order_no: '12345',
    scheduled: '08/16/16 13:45',
    lateral: 'L1-8-1-T7, L1-8-1-T6',
    start_time: '08/16/16 15:45',
    checks: '23.25',
    stop_time: '08/17/16 15:30', 
    status: 'Delivered',
    approx_cfs: '25.00',
    approx_hrs: '22',
    approx_af: '45.38',
    },

    {order_no: '12346',
    scheduled: '08/17/16 11:45',
    lateral: 'L1-8-1-T7, L1-8-1-T6',
    start_time: '08/17/16 15:30',
    checks: '20.25',
    stop_time: '', 
    status: 'Running',
    approx_cfs: '25.00',
    approx_hrs: '10',
    approx_af: '20.63',
    },

    {order_no: '12346',
    scheduled: '08/17/16 11:45',
    lateral: 'L1-8-1-T7, L1-8-1-T6',
    start_time: '08/17/16 15:30',
    checks: '20.25',
    stop_time: '', 
    status: 'Running',
    approx_cfs: '25.00',
    approx_hrs: '10',
    approx_af: '20.63',
    },

    {order_no: '12346',
    scheduled: '08/17/16 11:45',
    lateral: 'L1-8-1-T7, L1-8-1-T6',
    start_time: '08/17/16 15:30',
    checks: '20.25',
    stop_time: '', 
    status: 'Running',
    approx_cfs: '25.00',
    approx_hrs: '10',
    approx_af: '20.63',
    }
];

2 个答案:

答案 0 :(得分:2)

因为Date.parse()首先尝试将输入转换为数字,然后从中获取等效日期,并且在js中使用除“ - ”或“/”之类的数字之外的字符生成字符串将导致NaN结果Date.parse也将返回NaN。在这种情况下,您可以使用new Date(str: String)从有效日期字符串中生成日期对象。您还可以使用getTime()函数进行日期的数学运算。

let data = {
  order_no: '12346',
  scheduled: '08/17/16 11:45',
  lateral: 'L1-8-1-T7, L1-8-1-T6',
  start_time: '08/17/16 15:30',
  checks: '20.25',
  stop_time: '', 
  status: 'Running',
  approx_cfs: '25.00',
  approx_hrs: '10',
  approx_af: '20.63',
 };
this.order_no = data["order_no"] || "";
this.scheduled = data["scheduled"] || "";
this.lateral = data["lateral"] || "";
this.start_time = data["start_time"] || "";
this.checks = data["checks"] || "";
this.stop_time = data["stop_time"] || "";
this.status = data["status"] || "";
this.approx_cfs = data["approx_cfs"] || "";
this.approx_hrs = data["approx_hrs"] || "";
this.approx_af = data["approx_af"] || "";
this.calc = (!this.stop_time ? ((new Date().getTime() - new Date(this.start_time).getTime()) / 1000.0 / 60.0 / 60.0) * this.checks * 0.0825 : ((new Date(this.stop_time).getTime() - new Date(this.start_time).getTime()) /1000.0 / 60.0 / 60.0) * this.checks * 0.0825); 

console.log(calc);

答案 1 :(得分:0)

问题是您正在尝试将字符串类型值分配给检查类型为number的变量。

start_time: string; checks: number; stop_time: string;

指定值: -

start_time: '08/17/16 15:30', checks: '20.25', stop_time: '',