我已经用jspm安装了“interact.js”(为了使 typescript 感到高兴,还安装了npm)。该应用程序运行正常,但我的代码显示错误:

import { interact } from 'interact.js/interact'
// ==> typescript error: TS2307: Cannot find module 'interact.js/interact'

我认为问题与包含“.js”的npm模块有关,但我不确定。无论如何,有没有一种方法可以解决此问题

A.帮助 typescript 找到模块
B.禁用此特定错误(因为它可以正常工作)

PS:这是我的tsconfig.json文件:
{ "exclude":
  [ "node_modules"
  , "jspm_packages"
  , ".git"
  , "typings/browser"
  , "typings/browser.d.ts"
  ]
, "compilerOptions":
  { "outDir": "dist"
  , "target": "es5"
  , "sourceMap": true
  , "experimentalDecorators": true
  }
, "compileOnSave": false
}

最佳答案

TypeScript编译器/语言服务实际上并不像您期望的那样通过文件系统或package.json解析模块名称-it instead uses the definition ( .d.ts ) files that define the type information

尽管这不是世界上最直观的东西,但他们的理由并非完全没有道理-如果没有定义文件,就不可能知道要导入的东西是什么类型,而且他们对于使编译器默认为将导入设置为any类型。

简而言之,解决此问题的方法就是简单地安装定义文件(如果可用),或者写/存根自己的文件。 They'll be making this easier in TypeScript 2.0 by the sounds of it,但就目前情况而言,它需要非常多的代码来创建虚拟定义:

declare module "interact.js/interact" {
    export var interact: any;
}

07-24 09:47
查看更多