使用类似包的类型定义

我在Typescript应用程序中使用fabric-with-gestures,一切都很好。 但是,原始库fabric具有定义的类型,我可以使用npm install --dev @types/fabric安装它们,但是Typescript无法识别fabric-with-gestures软件包中的它们。

由于没有模块fabric,并且我假设类型基本相同,我如何使用fabric-with-gestures@types/fabric-with-gestures的类型? 这有可能吗?

o345768478 回答:使用类似包的类型定义

一般来说,对于所有软件包,您都可以通过在baseUrl中添加pathstsconfig.json选项来创建类型别名(我不使用fabric,但是快速设置确实编译成功了。

tsconfig.json:

{
  "compilerOptions": {
    ...
    // absolute name imports are resolved relative to baseUrl
    "baseUrl": ".",// alias "fabric-with-gestures" is mapped to path of fabric @types
    "paths": { 
      "fabric-with-gestures": ["node_modules/@types/fabric"]
    }
  }
}

用法:

// Use fabric-with-gestures. Types are internally mapped to @types/fabric
import { fabric } from "fabric-with-gestures"; 
本文链接:https://www.f2er.com/3093998.html

大家都在问