打字稿声明文件 - "导出声明"之间的区别和"出口"

时间:2017-01-20 01:29:41

标签: typescript

我正在学习打字稿。在我研究一些源文件时,我发现有时会有export declare ...,有时在声明文件export ...中只有.d.ts

经过一些测试,在我看来它们是等价的。没有declare没有区别。

我知道在没有declare的情况下需要export。例如,declare var test:any;

但是export declareexport是等价的吗?

附带问题:对declareinterface来说,type根本不需要.d.ts吗?在interface test{}文件中,如果我只放test,则declare在没有var express = require('express'); var app = express(); app.set('view engine', 'ejs'); // Imports the Google Cloud client library const Translate = require('@google-cloud/translate'); // Your Google Cloud Platform project ID const projectId = process.env.GOOGLEPROJECTID; // Instantiates a client const translateClient = Translate({ projectId: projectId }); // The text to translate const text = 'Hello, world!'; // The target language const target = 'ru'; // Translates some text into Russian let russianTranslate = translateClient.translate(text, target) .then((results) => { const translation = results[0]; console.log(`Translation: ${translation}`); return {textToTranslate: `Text: ${text}`} }); app.get('/', function(req, res){ res.render('index', { translation: russianTranslate }); }); app.listen(8080, function() { console.log('App Started!'); }); 的地方可用。那是为什么?

1 个答案:

答案 0 :(得分:0)

declare标志告诉TypeScript类或接口的形状是在其他地方定义的,而不是在 here 处定义的;例如,可用于由CDN提供服务并由index.html文件中的<script>标签导入的类/接口/功能(当然还有其他事情)。

如果您正在创建应用程序中使用的类或接口,则不需要declare标志。

示例:

// my-cool-object.ts

export interface MyCoolObject {
// stuff in my object
}
// my-cool-project.ts

import { MyCoolObject } from './my-cool-object';  // or wherever the interface definition file is located;

const myObject: MyCoolObject;

// ...more code goes here
相关问题