简体   繁体   English

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

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

I'm using Vue.js with cli and starting project on Typescript. 我将Vue.js与cli一起使用,并在Typescript上启动项目。 I didn't have any problem with it except of cases when I have to use js libraries. 除了必须使用js库的情况外,我没有任何问题。 I know about this https://github.com/DefinitelyTyped/DefinitelyTyped and I use it when possible. 我知道这个https://github.com/DefinitelyTyped/DefinitelyTyped,并在可能的情况下使用它。 But there is one big problem when js library is new, old or unpopular and has no type definitions. 但是,当js库是新的,旧的或不受欢迎且没有类型定义时,存在一个大问题。 I spend a lot of time, looking for solution and it seems like there is only few of options: 我花费大量时间寻找解决方案,似乎只有很少的选择:

In my case first option is unavailable. 在我的情况下,第一种选择不可用。 Second - very depends on library, which may have difficult structure to declare. 第二-非常依赖于库,库可能难以声明。 So, what I can do if I want just import js library as is? 因此,如果我只想按原样导入js库,该怎么办? I tried these ways: 我尝试了以下方法:

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');

Also, tried add different variations and combinations with imports above like: 另外,尝试使用上面的导入添加不同的变体和组合,例如:

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

etc. And nothing, I just have errors in console like: 等。什么都没有,我只是在控制台中有错误,例如:

Uncaught ReferenceError: Velocity is not defined 未被捕获的ReferenceError:未定义速度

It's not my typo in code, in regular js same code works fine ie https://codepen.io/anon/pen/YLBZMd 这不是我的错字,在常规js中,相同的代码也可以正常工作,即https://codepen.io/anon/pen/YLBZMd

Another my suspicion is tscompiler and his rules/settings. 另一个我怀疑的是tscompiler及其规则/设置。 Here is mine: 这是我的:

{
  "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"
  ]
}

Can someone explain me what I'm doing wrong? 有人可以解释我在做什么吗? Why so simple thing causes so big problems? 为什么这么简单的事情会引起如此大的问题?

Uncaught ReferenceError: Velocity is not defined 未被捕获的ReferenceError:未定义速度

This is a runtime error not a compile time typescript error . 这是运行时错误,而不是编译时打字稿错误

Fix 固定

Make sure Velocity global is available. 确保全局Velocity可用。 eg add : https://raw.githubusercontent.com/julianshapiro/velocity/master/velocity.min.js as a script tag before your application code runs. 例如, 运行应用程序代码之前,https://raw.githubusercontent.com/julianshapiro/velocity/master/velocity.min.js添加为script标记。

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

相关问题 如何在没有模块声明的情况下导入Typescript - How to import in Typescript without module declaration 如何使用带脚本的js库? - How to use js libraries with typescript? 如何在 JavaScript 中使用/导入 TypeScript 声明文件? - How to use/import TypeScript declaration files in JavaScript? 如何在不要求/导入/导出的情况下为自定义类创建Typescript声明文件 - How to create a Typescript declaration file for a custom class without require/import/export 一般如何在 TypeScript 中导入库? - How do I import libraries in TypeScript in general? Typescript声明文件位于单独目录时,如何导入js模块? - How to import js module when Typescript declaration file is located in a separate directory? 如何在打字稿文件中导入没有定义文件的js库 - How to import a js library without definition file in typescript file 如何在不使用 systemJS 或捆绑 JS 的情况下使用 TypeScript 和 import 语句? - How to use TypeScript and import statements without systemJS or bundling JS? 如何利用Intellisense将js库导入Typescript模块/ VS Code任务 - How to import js libraries to Typescript module / VS Code task, leveraging Intellisense 如何在 TypeScript 中重用参数类型声明 - How to reuse argument type declaration in TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM