如何在TypeScript脚本中声明预先存在的变量?

时间:2018-08-06 18:29:19

标签: typescript

我有一个脚本,该脚本将使用预定义的绑定运行,例如response

我有一个TS界面,可以通知我的代码response可以做什么。

如何让TypeScript知道此预定义变量存在?

这是我想要实现的:

import { HttpResponse } from './response';

// FIXME this binding already exists in the context of this script
let response: HttpResponse

// use response with type-checking
console.log(response.statusCode);

1 个答案:

答案 0 :(得分:3)

您可以使用global scope augmentation使模块中的代码知道response是在全局范围内定义的。

import { HttpResponse } from './response';

declare global {
    let response: HttpResponse;
}