You need to enable JavaScript to run this app. TypeScript报错:'CwConfirmLotConfig'仅指类型,此处被当作值使用
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript报错:'CwConfirmLotConfig'仅指类型,此处被当作值使用

报错原因

TypeScript 中的 interface 属于纯类型声明,仅在编译阶段做类型校验用,编译为 JavaScript 后会被完全清除,不存在对应的实体值,因此不能使用 new 关键字进行实例化,你现在的写法就是把接口当成了类来调用,才会触发这个报错。

解决方法

你可以根据自己的业务需求二选一:

  • 方案1:仅需要符合类型结构的对象,不需要实例化能力
    直接创建符合CwConfirmLotConfig字段要求的对象即可,不需要用new,测试代码修改为:
    import { CwConfirmLotConfig } from './cw-confirm-lot-config.model';
    
    describe('CwConfirmLotConfig', () => {
      it('should create an instance', () => {
        // 按照接口字段定义赋值,不需要new
        const mockConfig: CwConfirmLotConfig = {
          monitorType: '',
          tempalteType: '',
          patternIdKeyword: '',
          tempalteId: '',
          createUser: '',
          updateUser: '',
          createDate: '',
          updateDate: ''
        };
        expect(mockConfig).toBeTruthy();
      });
    });
    
  • 方案2:确实需要通过new生成实例
    将原来的CwConfirmLotConfig接口改为类声明即可,修改cw-confirm-lot-config.model.ts的代码:
    // 把interface改成class,给字段设置默认值或者通过constructor传值
    export class CwConfirmLotConfig {
      monitorType: string = '';
      tempalteType: string = '';
      patternIdKeyword: string = '';
      tempalteId: string = '';
      createUser: string = '';
      updateUser: string = '';
      createDate: string = '';
      updateDate: string = '';
    }
    
    export interface CwConfirmLotConfigUpdateData {
      create: CwConfirmLotConfig[];
      update: CwConfirmLotConfig[];
      delete: CwConfirmLotConfig[];
    }
    
    修改后原来的测试代码不需要改动即可正常运行。
额外提示

你代码中多次出现tempalte的拼写错误,正确拼写为template,建议统一修正,避免后续业务调用时出现字段不匹配的问题。

内容的提问来源于stack exchange,提问作者Cait97

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 10:09:04