【问题标题】:How to install .d.ts for fabric如何为织物安装 .d.ts
【发布时间】:2017-07-21 17:20:37
【问题描述】:

我正在尝试为 fabric (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/fabric/) 安装类型定义,但我无法找出正确的类型命令。

一开始我尝试typings install dt~fabric --global --save 并得到以下错误: $typings install dt~fabric --global --save typings ERR! message Attempted to compile "fabric" as a global module, but it looks like an external module. You'll need to remove the global option to continue.

然后我尝试了typings install dt~fabric --save,typings 命令成功但typings/modules/fabric/index.d.ts 文件与原始文件https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/fabric/index.d.ts 不同。 typings 命令用declare module 'fabric' {} 包装原始内容,这会在.d.ts 文件内的语句export as namespace fabric; 处导致编译器错误Error:(9, 1) TS1316:Global module exports may only appear at top level.

打字版本 2.1.0

【问题讨论】:

  • 你试过npm install --save @types/fabric吗?
  • 没有@types/fabric
  • 我很确定有。你错过拼写了吗?
  • @AnyName 是的,你是对的......我现在找到了。但是又出现了一个问题,“Uncaught TypeError: fabric.Canvas is not a constructor”,比如stackoverflow.com/questions/36984795/…在使用webpack时,有什么想法吗?
  • 我解决了,我使用<script>标签并在webpack中包含"fabric": "fabric" externals,谢谢你的帮助

标签: typescript fabricjs


【解决方案1】:
npm install --save-dev @types/fabric

会成功的,per the NPM docs.

你可以看到type definitions here.

我在学习 Fabric 时使用了它,由于 IntelliSense,它对理解 API 有巨大的帮助。

此外,如果您在任何 *.js 文件的顶部使用 //@ts-check,它还会为您提供它认为正确的 IntelliSense 的粗略估计。

TypeScript 2.3 及更高版本支持使用 --checkJs 对 .js 文件进行类型检查和报告错误的模式。 The docs explain how it works.

【讨论】:

    猜你喜欢
    • 2016-09-01
    • 2016-05-14
    • 1970-01-01
    • 2020-12-12
    • 2018-03-04
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多