在打字稿中引用 javascript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26274278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:39:39  来源:igfitidea点击:

Reference javascript file inside typescript

javascriptreferencetypescript

提问by user1160771

I would like to use a javascript function inside a typescript file. How can I reference the javascript file containing that function inside typescript file?

我想在打字稿文件中使用 javascript 函数。如何在打字稿文件中引用包含该函数的 javascript 文件?

I tried

我试过

<reference path="../libs/myjsfile.js" />

this brings an error while building like: Cannot resolve referenced file: ../libs/myjsfile.js

这在构建时会带来错误,例如: Cannot resolve referenced file: ../libs/myjsfile.js

回答by Doug

While the two answers above will work for a small or single variable API, the correct answer is to write a type declaration file for your JavaScript file.

虽然上面的两个答案适用于小型或单变量 API,但正确的答案是为 JavaScript 文件编写类型声明文件。

The type declaration file for your example would be named myjsfile.d.ts, where the .d.tssuffix is what tells the TypeScript compiler that it's parsing a declaration file. If you use Visual Studio, your declaration file will be recognized by the TypeScript compiler as long as it is somewhere (anywhere) in the project you are working on.

您示例的类型声明文件将命名为 myjsfile.d.ts,其中.d.ts后缀告诉 TypeScript 编译器它正在解析声明文件。如果你使用 Visual Studio,你的声明文件将被 TypeScript 编译器识别,只要它在你正在处理的项目中的某个地方(任何地方)。

Declaration files store metadata about JavaScript variables, functions and objects so the TypeScript compiler can do its job. If your JavaScript file happens to be a library or framework in common use, DefinitelyTypedis definitely the place to find the definition file you need.

声明文件存储有关 JavaScript 变量、函数和对象的元数据,以便 TypeScript 编译器可以完成其工作。如果您的 JavaScript 文件恰好是常用的库或框架,那么绝对类型绝对是您找到所需定义文件的地方。

If your JavaScript is your own work or less well known, you'll have to write your own type declaration file. See the section Writing .d.ts filesin The TypeScript Handbook.

如果您的 JavaScript 是您自己的作品或鲜为人知,则您必须编写自己的类型声明文件。请参阅TypeScript 手册编写 .d.ts 文件部分。

If the name declaration file sounds intimidating, don't worry - they are far easier to write than programs. Also, remember that you don't need to specify all metadata for your JavaScript file, just the functionality you need.

如果名称声明文件听起来令人生畏,请不要担心 - 它们比程序更容易编写。另外,请记住,您不需要为 JavaScript 文件指定所有元数据,只需指定您需要的功能即可。

Let me know if you need any help!

如果您需要任何帮助,请告诉我!

回答by A. K-R

You just have to tell the compiler that the method exists outside typescript:

您只需要告诉编译器该方法存在于打字稿之外:

declare function myJavaScriptFunction(param1: number, param2: JQuery): void;

Be sure that the website loads all files needed, the normal js files and the js files that the typescript compiler generated

确保网站加载了所有需要的文件,普通的 js 文件和打字稿编译器生成的 js 文件

回答by basarat

this brings an error while building like: Cannot resolve referenced file: ../libs/myjsfile.js

这会在构建时出现错误,例如:无法解析引用的文件:../libs/myjsfile.js

Reference file tags are for TypeScriptnot JavaScript. You would load JavaScript using a script tag in your page (or something like requirejs).

参考文件标签用于TypeScript而不是 JavaScript。您将使用页面中的脚本标记(或类似 requirejs)加载 JavaScript。

To use the JavaScript from TypeScript you would declarethe API of the javascript you want to use for TypeScript consumption (as AKR pointed out).

要使用 TypeScript 中的 JavaScript,您需要声明要用于 TypeScript 消费的 JavaScript 的 API(如 AKR 所指出的)。

回答by Roman

You can see the latest version of the TypeScript documenttion in TypeScript Handbook on GitHub:

你可以在 GitHub 上的 TypeScript Handbook 中查看最新版本的 TypeScript 文档:

Also I am recomending you the tsdutility to search and install TypeScript definition files directly from the community driven DefinitelyTypedrepository. This repository contains definition files *.d.tsfor the most popular javascript libraries.

此外,我还向您推荐tsd实用程序,以直接从社区驱动的绝对类型存储库中搜索和安装 TypeScript 定义文件。此存储库包含最流行的 javascript 库的定义文件*.d.ts