首页 > 解决方案 > 如何忽略 TypeScript 错误 TS2339 - 类型上不存在属性

问题描述

我正在使用 Angular 8(在 Visual Studio 2019 中启动了一个项目)并拥有fabric.init.ts包含下一个内容的文件:

import 'fabric';
import * as fabric from 'fabric/fabric-impl';

// (TS) Property 'DPI' does not exist on type 'typeof import(..@types/fabric/fabric-impl)'
fabric.DPI = 213;
....

Next 没问题,因为它符合 d.ts 定义:

// Override fabric to meet ourrequirements
fabric.util.object.extend(fabric.Object.prototype, {
  centeredRotation: true,

如何忽略DPITS2339 错误。拥有自动补全功能真的很棒,特别是对于学习结构。

我的 package.json:

"private": true,
"dependencies": {
  "@angular/core": "8.2.14",
  ....
  "@angular/router": "8.2.14",
  "@types/fabric": "3.5.1",
  "fabric": "3.5.1",
  ...
},

编辑

现在我通过添加这一行来解决它:

// @ts-ignore
fabric.DPI = 213;

我知道这不是最好的解决方案,我更喜欢@wentjun 的解决方案。

通过改变这个:我现在也启用了 VisualStudio 智能感知import * as fabric from 'fabric/fabric-impl';import { fabric } from 'fabric';当然,我在 package.json 中添加了@types/fabricjs。

在此处输入图像描述

标签: angulartypescriptfabricjs

解决方案


比如像这样(fabric as any).DPI = 213;


推荐阅读