disable wrong typescript error

这一生的挚爱 提交于 2019-12-31 01:48:12

问题


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'

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. Help Typescript find the module B. Disable this specific error (since it works fine)

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
}

回答1:


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.

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.

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;
}


来源:https://stackoverflow.com/questions/36737955/disable-wrong-typescript-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!