简体   繁体   English

禁用错误的打字稿错误

[英]disable wrong typescript error

I have installed 'interact.js' with jspm (and npm for typescript to be happy). 我已经用jspm安装了'interact.js'(并且npm for typescript很高兴)。 The app runs fine but my code shows errors: 该应用运行良好,但我的代码显示错误:

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

I suppose the problem has something to do with the npm module containing '.js' but I am not sure. 我想这个问题与包含'.js'的npm模块有关,但我不确定。 Anyway, is there a way to fix this either by 无论如何,有没有办法解决这个问题

A. Help Typescript find the module B. Disable this specific error (since it works fine) A.帮助打字稿找到模块B.禁用此特定错误(因为它工作正常)

PS: here is my tsconfig.json file: 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
}

The TypeScript compiler/language service doesn't actually resolve module names through the filesystem or your package.json like you might expect - it instead uses the definition ( .d.ts ) files that define the type information . TypeScript编译器/语言服务实际上并不像您期望的那样通过文件系统或package.json解析模块名称 - 而是使用定义类型信息的定义( .d.ts )文件

While it's not the most intuitive thing in the world, their reasoning for it wasn't entirely unreasonable - without a definition file, it's impossible to know what type the thing being imported is, and they were somewhat cagey about making the compiler default to just setting imports to the any type. 虽然它不是世界上最直观的东西,但它们的推理并非完全不合理 - 如果没有定义文件,就不可能知道导入的东西是什么类型,并且他们对于使编译器默认为只是有些谨慎将导入设置为any类型。

So in short, the solution to this problem is simply to install the definition files if available, or write/stub out your own if not. 简而言之,这个问题的解决方案只是安装定义文件(如果可用),或者编写/存根自己(如果没有)。 They'll be making this easier in TypeScript 2.0 by the sounds of it , but even as it stands, it takes very code to create a dummy definition: 他们会在TypeScript 2.0中通过它的声音使这变得更容易 ,但即便如此,创建虚拟定义需要非常代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM