You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript实例化接口数组时无法识别属性的问题求助

TypeScript实例化接口数组时无法识别属性的问题求助

各位大佬好,我在Angular组件里实现自定义接口的时候遇到了一个棘手的问题,TS编译器一直高亮数组里的第一个属性,鼠标hover上去还报错,折腾了半天没找到原因,来求助大家!

首先是我定义的Device接口:

export interface Device {
  constructor(
    deviceID: string,
    deviceName: string,
    deviceModel: string,
    manufactureYear: string,
    deviceOS: string
  );
}

然后是我的自定义组件代码:

import { Component, OnInit} from "@angular/core";
import { Device } from "src/shared/interfaces/device";

@Component({
  selector: 'device-dashboard',
  templateUrl: './device-dashboard.component.html',
  styleUrls: ['./device-dashboard.component.less']
})
export class DeviceDashBoardComponent implements OnInit{
  constructor() {}

  devices: Device[] = [
    {
      deviceID: "12345",
      deviceName: 'name',
      deviceModel: 'model',
      manufactureYear: '2015',
      deviceOS: 'Ubuntu 22.04',
    },
  ];

  ngOnInit(): void {     }
}

编译器弹出的错误提示是:

Type '{ deviceID: string; deviceName: string; deviceModel: string; manufactureYear: string; deviceOS: string; }' is not assignable to type 'Device'.
Object literal may only specify known properties, and 'deviceID' does not exist in type 'Device'.

我明明在接口里定义了这些属性啊,为啥编译器说deviceID不存在呢?实在搞不懂,求各位帮忙分析下问题出在哪!

备注:内容来源于stack exchange,提问作者liberlux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 10:24:07