网站搜索

angular - 如何忽略 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;
....

下一个是可以的,因为它与 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。

答案#1

要么按照 Vugar 的建议将其类型转换为 any,然后 TypeScript 就会忽略它,要么

导入 Typescript 定义

答案#2

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

相关文章