angular - 如何忽略 TypeScript 错误 TS2339 - 类型上不存在属性

标签 angular typescript fabricjs

我正在使用 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;
....

接下来就可以了,因为它匹配 d.ts 定义:
// Override fabric to meet ourrequirements
fabric.util.object.extend(fabric.Object.prototype, {
  centeredRotation: true,

如何忽略 DPI TS2339 错误。拥有自动完成功能真的很棒,特别是对于学习 Fabric 。

我的 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 的解决方案。

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

enter image description here

最佳答案

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

关于angular - 如何忽略 TypeScript 错误 TS2339 - 类型上不存在属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631075/

相关文章:

Angular2 获取动态创建输入的值

css - Ionic 5 在 iOS 上设置可折叠标题颜色

angular - ng-select 在 Angular 6 中以编程方式删除元素

javascript - 当文件名通过 props 传递时,React 组件无法动态 require() 图像文件

javascript - 找不到模块 'on-finished'

javascript - 从 Canvas 中删除图像 - fabricjs (javascript)

json - Fabric js-在保存和加载回 Canvas 时缺少添加附加属性的扩展 toObject 方法

javascript - D3 可视化 - 在 ngOnChanges 中更新(Angular/TypeScript)

javascript - 如何在不使用任何引用的情况下编译 TypeScript

javascript - 在 fabricjs 上下文中更改文本不起作用