简体   繁体   English

你什么时候在 TypeScript 中使用声明?

[英]When do you use declare in TypeScript?

In TypeScript, why do you sometimes need to use declare for declaring a variable, and sometimes you don't (the same question is true for functions, …)?在 TypeScript 中,为什么有时需要使用declare来声明变量,有时不需要(同样的问题对于函数也是如此,......)?

To give an example: When (and why) do I use举个例子:我什么时候(以及为什么)使用

declare var foo: number;

if如果

let foo: number;

would do the same (at least it seems to me as if it did the same, ie they both declare a variable called foo of type number ).会做同样的事情(至少在我看来好像它做了同样的事情,即它们都声明了一个名为foo类型为number的变量)。 What's the difference?有什么不同?

You never use declare to declare a variable.你从不使用declare声明一个变量。 You just use it to let TypeScript know that the variable exists, even though it's not declared in the code (for instance, because it's a global declared in other code, or because you're going to combine the JavaScript output of tsc with another file that declares the variable).您只需使用它让 TypeScript 知道该变量存在,即使它没有在代码中声明(例如,因为它是在其他代码中声明的全局变量,或者因为您要将tsc的 JavaScript 输出与另一个文件结合起来)声明变量)。 Or put it another way: It only declares it for the TypeScript compiler, not for the JavaScript runtime.或者换句话说:它只为 TypeScript 编译器声明它,而不是为 JavaScript 运行时声明。

If you use the playground to compile your declare var foo: number;如果您使用 Playground 编译您的declare var foo: number; , it literally outputs nothing for that declaration; ,它实际上不为该声明输出任何内容; example . 例子

In contrast, let foo: number;相比之下, let foo: number; (or var foo: number; ) is a variable declaration; (或var foo: number; )一个变量声明; example . 例子

打字稿中的声明关键字对于告诉打字稿编译器在其他地方定义了声明很有用,希望此链接有帮助: http : //blogs.microsoft.co.il/gilf/2013/07/22/quick-tip-typescript -声明关键字/

In TypeScript, " declare var foo: number; " means: " In the code there is a number called foo. I know that you would not retrieve it. But please trust me and act just like you know it. " It is a kind of forward declaration just like you can see it in C/C++.在 TypeScript 中,“ declare var foo: number; ”的意思是:“在代码中有一个数字叫做 foo。我知道你不会检索它。但请相信我,像你知道的那样行事。 ”这是一种就像您在 C/C++ 中看到的那样的前向声明

Example:例子:

Assume that you are writing a TS script using jQuery.假设您正在使用 jQuery 编写 TS 脚本。 You would include all the JavaScript like this somewhere in your HTML page:您可以在 HTML 页面的某处包含所有这样的 JavaScript:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="myscript.js"></script> <!-- generated from TypeScript, with something like "tsc myscript.ts" -->

As a consequence, there is no need to include jQuery in your TS script, since JS generated from your TS script will know everything about jQuery's stuff.因此,无需在您的 TS 脚本中包含 jQuery,因为从您的 TS 脚本生成的 JS 将了解有关 jQuery 内容的所有内容。 However, the TS transpiler will remark jQuery's iconic " $() " a bunch of time in your code.但是,TS 转译器会在您的代码中多次注释 jQuery 标志性的“ $() ”。 You do not include jQuery in your TS code (because there is no need to) so it does not know anything about " $() ".您没有在 TS 代码中包含 jQuery(因为没有必要),因此它对“ $() ”一无所知。 It will consequently report to you lots of errors asking you what the hell is this " $ " it sees everywhere in the code.因此,它会向您报告许多错误,询问您在代码中随处可见的“ $ ”到底是什么。 To solve all those $ -related errors, you will write " declare var $: jQuery; " at the beginning of your TS script.要解决所有与$相关的错误,您将在 TS 脚本的开头编写“ declare var $: jQuery; ”。 By writing this, you tell this to the TS transpiler:通过写这个,你告诉 TS 转译器:

In the code there is a variable called " $ " whose type is " jQuery ".在代码中有一个名为“ $ ”的变量,它的类型是“ jQuery ”。 I know that you would not have retrieved it.我知道你不会找回它。 But assume that it exists and please act like there was a " import "aTSfile.ts"; " which perfectly describes it to you (a description just like I use it) earlier in the code.但是假设它存在并且请表现得像在代码前面有一个“ import "aTSfile.ts"; ”它完美地向您描述了它(就像我使用的描述一样)。 Trust me, JavaScript will know what that " $ " REALLY is when the script is executed.相信我,JavaScript就知道“什么$ ”执行脚本时确实是。 Do not worry about that.不用担心那个。 ;-) ;-)

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

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