typescript 打字稿中的 *.d.ts 与 *.ts 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29196657/
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-10-21 02:58:37  来源:igfitidea点击:

What is the difference between *.d.ts vs *.ts in typescript?

typescripttypescript1.4

提问by Nur Rony

I start playing with TypeScript which I found really awesome. But I am confused about the difference between *.d.tsand *.ts. What's the difference between them? can anybody explain me with proper example?

我开始玩 TypeScript,我发现它真的很棒。但我感到困惑的区别*.d.ts*.ts。它们之间有什么区别?有人可以用正确的例子解释我吗?

回答by David Sherret

TypeScript definition file (*.d.ts)

TypeScript 定义文件 ( *.d.ts)

These files are used for describing the "shape" of a JavaScript file for use in TypeScript.

这些文件用于描述在 TypeScript 中使用的 JavaScript 文件的“形状”。

For example, say I have the following JavaScript (Example.js):

例如,假设我有以下 JavaScript(Example.js):

function displayMessage(message) {
    alert(message);
}

With this file alone, my TypeScript code won't have any clue this function exists. It won't know its name and it won't know its parameters. We can fix this by describing it in a definition file as such (Example.d.ts):

单独使用这个文件,我的 TypeScript 代码不会知道这个函数是否存在。它不会知道它的名字,也不会知道它的参数。我们可以通过在定义文件中描述它来解决这个问题(Example.d.ts):

declare function displayMessage(message: string);

Now I can use the function displayMessagein TypeScript without compile errors and I'll get compile errors when I use it incorrectly (for example, if I supplied 2arguments instead of 1I would get an error).

现在我可以displayMessage在 TypeScript 中使用该函数而不会出现编译错误,并且当我使用它不正确时我会得到编译错误(例如,如果我提供了2参数而不是1我会得到一个错误)。

In short:Definition files allow you to use existing JavaScript code in TypeScript without having to rewrite the code in TypeScript.

简而言之:定义文件允许您在 TypeScript 中使用现有的 JavaScript 代码,而无需在 TypeScript 中重写代码。

TypeScript file (.ts)

打字稿文件 ( .ts)

This is the standard file extension you use when writing TypeScript. It will be compiled to JavaScript.

这是您在编写 TypeScript 时使用的标准文件扩展名。它将被编译为 JavaScript。

回答by Daniel Earwicker

Anything allowed in a *.d.ts file may also appear in a *.ts file, but not the reverse. So *.d.ts allows a subset of TypeScript's features.

*.d.ts 文件中允许的任何内容也可能出现在 *.ts 文件中,但反之则不然。所以 *.d.ts 允许 TypeScript 功能的子集。

A *.d.ts file is only allowed to contain TypeScript code that doesn't generate any JavaScript code in the output. If you attempt to use any feature of TypeScript that would generate JavaScript, you'll get an error.

*.d.ts 文件只允许包含不会在输出中生成任何 JavaScript 代码的 TypeScript 代码。如果您尝试使用任何会生成 JavaScript 的 TypeScript 功能,您将收到错误消息。

Interfaces are allowed, because they disappear completely after compilation.

接口是允许的,因为它们在编译后完全消失。

Const enums (added in 1.4) are also allowed, unlike ordinary enums which generate an object in the output JavaScript.

还允许使用常量枚举(在 1.4 中添加),这与在输出 JavaScript 中生成对象的普通枚举不同。

Top level classes, variables, modules and functions must be prefixed with declare. Often you'll see a top-level declare moduleand the stuff inside it is therefore also pure declaration:

顶级类、变量、模块和函数必须以declare. 通常你会看到一个顶层declare module,因此它里面的东西也是纯粹的声明:

declare module Something {
    var x;
}

They are useful for more than just exposing a TypeScript interface to code written in JavaScript. You can also use them to declare a set of common interfaces that are widely used in your code, so it is not necessary to requirea specific physical module just to get visibility of those interfaces.

它们不仅仅用于将 TypeScript 接口暴露给用 JavaScript 编写的代码。您还可以使用它们来声明一组在您的代码中广泛使用的通用接口,因此不需要require特定的物理模块只是为了获得这些接口的可见性。