简体   繁体   English

如何在打字稿文件中导入没有定义文件的js库

[英]How to import a js library without definition file in typescript file

I want to switch from JavaScript to TypeScript to help with code management as our project gets larger.随着我们的项目越来越大,我想从 JavaScript 切换到 TypeScript 以帮助进行代码管理。 We utilize, however, lots of libraries as amd Modules, which we do not want to convert to TypeScript.但是,我们将许多库用作 amd 模块,我们不想将其转换为 TypeScript。

We still want to import them into TypeScript files, but we also do not want to generate definition files.我们仍然想将它们导入到 TypeScript 文件中,但我们也不想生成定义文件。 How can we achieve that?我们怎样才能做到这一点?

eg The new Typescript file:例如新的打字稿文件:

/// <reference path="../../../../definetelyTyped/jquery.d.ts" />
/// <reference path="../../../../definetelyTyped/require.d.ts" />
import $ = require('jquery');
import alert = require('lib/errorInfoHandler');

Here, lib/errorInfoHandler is an amd module included in a huge JavaScript library that we do not want to touch.这里, lib/errorInfoHandler是一个 amd 模块,包含在我们不想接触的巨大 JavaScript 库中。

Using the above code produces the following errors:使用上面的代码会产生以下错误:

Unable to resolve external module ''lib/errorInfoHandler'' 
Module cannot be aliased to a non-module type.

This should actually produce the following code:这实际上应该产生以下代码:

define(["require", "exports", "jquery", "lib/errorInfoHandler"], function(require, exports, $, alert) {
...

}

Is there a way to import a JavaScript library into TypeScript as an amd Module and use it inside the TypeScript file without making a definition file?有没有办法将 JavaScript 库作为 amd 模块导入 TypeScript 并在 TypeScript 文件中使用它而不制作定义文件?

A combination of the 2 answers given here worked for me.此处给出的 2 个答案的组合对我有用。

//errorInfoHandler.d.ts
declare module "lib/errorInfoHandler" {
   var noTypeInfoYet: any; // any var name here really
   export = noTypeInfoYet;
}

I'm still new to TypeScript but it looks as if this is just a way to tell TypeScript to leave off by exporting a dummy variable with no type information on it.我仍然是 TypeScript 的新手,但看起来这只是通过导出一个没有类型信息的虚拟变量来告诉 TypeScript 停止的一种方式。

EDIT编辑

It has been noted in the comments for this answer that you can achieve the same result by simply declaring:在此答案的评论中已经指出,您只需声明即可获得相同的结果:

//errorInfoHandler.d.ts
declare module "*";

See the github comment here .请参阅此处的 github 评论。

Either create your own definition file with following content:使用以下内容创建自己的定义文件:

declare module "lib/errorInfoHandler" {}

And reference this file where you want to use the import.并在要使用导入的位置引用此文件。

Or add the following line to the top of your file:或者将以下行添加到文件顶部:

/// <amd-dependency path="lib/errorInfoHandler">

Note: I do not know if the latter still works, it's how I initially worked with missing AMD dependencies.注意:我不知道后者是否仍然有效,这是我最初处理缺少的 AMD 依赖项的方式。 Please also note that with this approach you will not have IntelliSense for that file.另请注意,使用这种方法,您将不会拥有该文件的 IntelliSense。

Create a file in lib called errorInfoHandler.d.ts .lib创建一个名为errorInfoHandler.d.ts的文件。 There, write:在那里,写:

var noTypeInfoYet: any; // any var name here really
export = noTypeInfoYet;

Now the alert import will succeed and be of type any .现在alert导入将成功并且是any类型。

Typically if you just want to need a temporary-faster-solution , that could be done by defining a new index.d.ts in the root of the project folder, then make a module name like described inside package.json file通常,如果您只需要一个临时更快的解决方案,可以通过在项目文件夹的根目录中定义一个新的index.d.ts来完成,然后创建一个模块名称,如package.json文件中所述

for example例如

// somefile.ts
import Foo from '@awesome/my-module'

// index.d.ts on @awesome/my-module
declare module '@awesome/my-module' {
  const bind: any;
  export default bind;
}

Ran into that that problem in 2020, and found an easy solution:在 2020 年遇到了那个问题,并找到了一个简单的解决方案:

  1. Create a decs.d.ts file in the root of your TS project.在 TS 项目的根目录中创建一个decs.d.ts文件。

  2. Place this declaration:放置此声明:

     declare module 'lib/errorInfoHandler';

This eliminates the error in my case.这消除了我的情况下的错误。 I'm using TypeScript 3.9.7我正在使用 TypeScript 3.9.7

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

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