禁用错误的打字稿错误 [英] disable wrong typescript error

查看:109
本文介绍了禁用错误的打字稿错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用jspm安装了'interact.js'(并且npm for typescript很高兴)。该应用运行正常,但我的代码显示错误:

I have installed 'interact.js' with jspm (and npm for typescript to be happy). The app runs fine but my code shows errors:

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

我想这个问题有点像与包含'.js'的npm模块有关,但我不确定。无论如何,有没有办法通过

I suppose the problem has something to do with the npm module containing '.js' but I am not sure. Anyway, is there a way to fix this either by

A来解决这个问题。帮助Typescript找到模块
B.禁用此特定错误(因为它工作正常)

A. Help Typescript find the module B. Disable this specific error (since it works fine)

PS:这是我的tsconfig.json文件:

PS: here is my tsconfig.json file:

{ "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 来解析模块名称 - 就像你想象的那样 - 它使用定义类型信息的定义( .d.ts )文件

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.

虽然它不是最直观的东西rld,他们对它的推理并非完全不合理 - 如果没有定义文件,就不可能知道导入的东西是什么类型,并且他们在使编译器默认为将导入设置为任何类型。

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.

所以简而言之,这个问题的解决方案就是安装定义文件(如果可用),如果没有,则自行编写/存根。 他们将通过它的声音在TypeScript 2.0中轻松实现这一点,但即使如此它代表了创建虚拟定义需要非常代码:

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:

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

这篇关于禁用错误的打字稿错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆