繁体   English   中英

如何在Typescript中粗略导入.js库而不进行类型声明?

[英]How to roughly import .js libraries without type declaration in typescript?

我将Vue.js与cli一起使用,并在Typescript上启动项目。 除了必须使用js库的情况外,我没有任何问题。 我知道这个https://github.com/DefinitelyTyped/DefinitelyTyped,并在可能的情况下使用它。 但是,当js库是新的,旧的或不受欢迎且没有类型定义时,存在一个大问题。 我花费大量时间寻找解决方案,似乎只有很少的选择:

在我的情况下,第一种选择不可用。 第二-非常依赖于库,库可能难以声明。 因此,如果我只想按原样导入js库,该怎么办? 我尝试了以下方法:

import { } from 'velocity-animate';
import Velocity from 'velocity-animate';
import { Velocity } from 'velocity-animate';
import Velocity from '../../node_modules/velocity-animate';
import * as Velocity from 'velocity-animate';
const Velocity = require('velocity-animate');
const Velocity: any = require('velocity-animate');
const Velocity = require('../../node_modules/velocity-animate/velocity.js');

另外,尝试使用上面的导入添加不同的变体和组合,例如:

declare var Velocity: any;
declare module 'velocity-animate';

等。什么都没有,我只是在控制台中有错误,例如:

未被捕获的ReferenceError:未定义速度

这不是我的错字,在常规js中,相同的代码也可以正常工作,即https://codepen.io/anon/pen/YLBZMd

另一个我怀疑的是tscompiler及其规则/设置。 这是我的:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "es2015",
    "strict": true,
    "allowJs": true,
    "jsx": "preserve",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "noImplicitAny": false,
    "types": [
      "node",
      "googlemaps"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    }
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.vue",
    "tests/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

有人可以解释我在做什么吗? 为什么这么简单的事情会引起如此大的问题?

未被捕获的ReferenceError:未定义速度

这是运行时错误,而不是编译时打字稿错误

固定

确保全局Velocity可用。 例如, 运行应用程序代码之前,https://raw.githubusercontent.com/julianshapiro/velocity/master/velocity.min.js添加为script标记。

暂无
暂无

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

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