简体   繁体   English

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

[英]error TS2322: Object literal may only specify known properties, and 'labels' does not exist in type

I have a problem that I can not explain. 我有一个我无法解释的问题。

I create an angular service with the interface implementation but it tells me that I have an error that I can not explain. 我使用接口实现创建了一个角度服务,但它告诉我我无法解释一个错误。

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

Object literal may only specify known properties, and 'labels' does not exist in type 'DataBase[]'. 对象文字只能指定已知的属性,而'label'在'DataBase []'类型中不存在。

this my code : 这是我的代码:

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'
        ]
       }]
    }
  }
  }

Has anyone ever had this problem and this error message? 有没有人遇到过此问题和此错误消息?

Because I do not see where the problem comes from 因为我看不出问题出在哪里

datasets in DataBase looks like an Array type when you've declared it as an Object type. 当您将其声明为对象类型时, DataBase datasets看起来就像一个Array类型。

Change your DataBase interface to this: 将您的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[];
}

Here's a Working Sample StackBlitz for your ref. 这是供您参考的工作样本StackBlitz

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象字面量只能指定已知的属性,并且类型“设置”中不存在“选择” - Object literal may only specify known properties, and 'select' does not exist in type 'Settings' Object 文字可能只指定已知属性,并且“设置”类型中不存在“按钮” - Object literal may only specify known properties, and 'buttons' does not exist in type 'Settings 对象文字只能指定已知的属性,并且'ChatMessage []'类型中不存在'message' - Object literal may only specify known properties and 'message' does not exist in type 'ChatMessage[]' 对象文字只能指定已知属性 - Object Literal May Only Specify Known Properties 错误:对象字面量只能以角度指定已知属性 - error: Object literal may only specify known properties in angular 对象文字可能只指定已知属性错误 - Object literal may only specify known properties error Angular Object文字只能指定知道属性,而...在类型中不存在 - Angular Object literal may only specify know properties and … does not exist in type 错误TS2322:无法将类型“对象”分配给类型“接触”。 角 - error TS2322: Type 'Object' is not assignable to type 'Contact'. Angular 错误 TS2322:“对象”类型不可分配给“产品”类型 - Error TS2322: Type 'Object' is not assignable to type 'Produit' 错误TS2322:类型'对象'不能分配给'电影'类型? Angular 7 - error TS2322: Type 'Object' is not assignable to type 'Movie'? Angular 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM