【问题标题】:How to ignore TypeScript error TS2339 - Property does not exist on type如何忽略 TypeScript 错误 TS2339 - 类型上不存在属性
【发布时间】:2020-04-25 02:48:10
【问题描述】:

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

下一步就ok了,因为它符合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 的解决方案。

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

【问题讨论】:

    标签: angular typescript fabricjs


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      将其转换为任何类型,然后 Typescript 将像 Vugar 建议的那样忽略它或

      导入Typescript definitions

      【讨论】:

      【解决方案3】:

      我不熟悉 Fabric,但推荐的做法是扩展 fabric 命名空间以接受 DPI 作为有效属性。

      首先,您可以创建一个打字稿文件并定义您的新结构定义。

      custom-fabric.ts:

      interface ExtendedFabric {
        DPI: number;
      }
      
      // apply additional types to fabric and export it for usage
      export const CustomFabric: (ExtendedFabric & typeof fabric) = fabric as any;
      

      在你使用它的类/组件中,

      import { CustomFabric } from './custom-fabric';
      
      CustomFabric.DPI = 213; // no TS errors
      

      【讨论】:

        猜你喜欢
        • 2016-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 2018-01-14
        • 2018-03-25
        • 1970-01-01
        相关资源
        最近更新 更多