为什么在语句中我们需要使用 declare
关键词?
export declare class Actions {
...
}
为什么在语句中我们需要使用 declare
关键词?
export declare class Actions {
...
}
找到了我正在寻找的东西:
var
创建一个新变量,declare
用于告诉 TypeScript 这个变量已经在其他地方被创建。如果使用 declare
,则不会添加任何生成的 JavaScript 代码中 - 它只是编译器的提示。
例如,如果您使用定义了 var externalModule
的外部脚本,则需要使用 declare var externalModule
来提示 TypeScript 编译器已经设置好了 externalModule
。
externalModule
和其他变量。为什么在运行时externalModule
未定义,但其他一些变量却不是呢? - Alejandro GonzálezTypeScript的declare关键字用于声明可能不是来自TypeScript文件的变量。
For example, lets imagine that we have a library called myLibrary that doesn’t have a TypeScript declaration file and have a namespace called myLibrary in the global namespace. If you want to use that library in your TypeScript code, you can use the following code:
declare var myLibrary;
The type that the TypeScript runtime will give to myLibrary variable is the any type. The problem here is that you won’t have Intellisense for that variable in design time but you will be able to use the library in your code. Another option to have the same behavior without using the declare keyword is just using a variable with the any type:
var myLibrary: any;
Both of the code examples will result in the same JavaScript output but the declare example is more readable and expresses an ambient declaration.
export declare class Action{
// ...
}
declare var myLibrary
不会被转译成任何内容:http://www.typescriptlang.org/play/#code/CYUwxgNghgTiAEEQBd4DMD2GBcBnZMAlgHYDmA3AFBKoBGs8AvPAORQtWWVA - apollotypescript中的declare关键字:
declare
关键字在 TypeScript 中非常有用,可以告诉 TypeScript 编译器某个声明是在别处定义的(可能是写在外部的 JavaScript 文件中或者属于运行时环境的一部分)。
假设我们有一个名为 foo 的变量是在其他地方声明的。当我们尝试引用该变量时,TypeScript 编译器会抛出一个错误:
foo = 'random'; // Error: 'foo' is not defined
我们可以使用 declare
关键字来解决这个问题:
declare var foo: string;
foo = 'random'; // no error anymore
以下是其影响:
foo
,可能会出现运行时错误。因此,仅在您知道变量在此处可用时使用declare
关键字。在这种特定情况下,declare 关键字的作用是:
export declare class Actions {
...
}
... 显然是没有用的,我认为 TypeScript 应该考虑将其变成错误(我不知道是否有隐藏的原因)。如果你声明一个类,你将永远不需要导入它。如果你导出一个期望别人导入的类,你不需要声明它。并且因为你正在声明这个类,根据定义,这个类应该可以在不需要导入它的情况下使用。但当你导出声明一个类时,这并不是真实的。你需要导入它才能使用。
简而言之:
export declare class Actions {
...
}
等同于
declare class Actions {
...
}
import
,而后者则不需要。 - Clocher Zhongdeclare
可以在没有任何导入或导出关键字的情况下自动定义TypeScript需要的声明文件,这是一个有用的功能,可以为遗留模块(npm安装的没有TypeScript定义的包)添加类型。
import
/export
是使用模块的正确方式,需要手动导入所有内容(逻辑和定义),而这可能有些繁琐。
实际应用中,export declare
允许你避免导出所有子元素,例如:
export declare namespace Redux {
namespace Store {
interface Definition { ... }
}
}
可能比以下内容更易读:
export namespace Redux {
export namespace Store {
export interface Definition { ... }
}
}
在这两种情况下,外部导入
是相同的(例如:import { Redux } from 'definitions/redux';
),我不知道这是否是一个好习惯,但我认为它很整洁!^^
重要的是要记住,向文件添加import
或export
将使其成为模块,因此declare
范围将不再处于全局级别。
附注,有一个错误(issue 16671):如果您在声明中使用const enum
(我在redux actions类型中使用它),并且指定了transpileOnly
标志(create-react-app-typescript软件包会这样做,所以我知道),则该枚举将不会被内联!你可能会遇到这个问题,也可能不会,但事先了解这一点很有用!