错误TS2322:对象文字只能指定已知属性,而类型中不存在“标签”

问题描述 投票:-1回答:1

我有一个我无法解释的问题。

我使用接口实现创建了一个角度服务,但它告诉我,我有一个我无法解释的错误。

error TS2322: Type '{ labels: string[]; datasets: { data: number[]; backgroundColor: string[]; hoverBackgroundColor: string[]; }[]; }' is not assignable to type 'DataBase[]'.

对象文字只能指定已知属性,'DataBase []'类型中不存在'标签'。

我的代码:

Interface.ts

export interface Dashboardtwidget {
  title: string;
  widgetType: string;
  datatype: DataBase[];
}

export interface DataBase {
 labels: string[];
 datasets: {
 data: number[];
 backgroundColor: string[];
 hoverBackgroundColor: string[]
};
}

service.ts

import {Injectable} from '@angular/core';
import {Dashboardtwidget} from '../models/dashboard';


@Injectable()
 export class DashboardService {
  data: Dashboardtwidget[] = [
  {
    title: 'Widget 1',
    widgetType: 'cardStyle1',
    datatype: {
      labels: ['A','B','C'],
      datasets: [
        {
         data: [300, 50, 100],
         backgroundColor: [
          '#FF6384',
          '#36A2EB',
          '#FFCE56'
         ],
         hoverBackgroundColor: [
          '#FF6384',
          '#36A2EB',
          '#FFCE56'
        ]
       }]
    }
  }
  }

有没有人有过这个问题和这个错误信息?

因为我没有看到问题的来源

angular typescript angular-services
1个回答
1
投票

datasets中的DataBase在宣布为对象类型时看起来像Array类型。

将您的DataBase界面更改为:

export interface Dashboardtwidget {
  title: string;
  widgetType: string;
  datatype: DataBase[];
}

interface Dataset {
  data: number[];
  backgroundColor: string[];
  hoverBackgroundColor: string[];
}

export interface DataBase {
  labels: string[];
  datasets: Dataset[];
}

这是你的参考的Working Sample StackBlitz

© www.soinside.com 2019 - 2024. All rights reserved.