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

如何在TypeScript脚本中声明预先存在的变量?,typescript,Typescript,我有一个脚本,它将使用预定义的绑定运行,比如response 我有一个TS接口,它通知我的代码我可以用响应做什么 如何让TypeScript知道这个预定义变量存在 这就是我想要实现的目标: import { HttpResponse } from './response'; // FIXME this binding already exists in the context of this script let response: HttpResponse // use response

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

我有一个TS接口,它通知我的代码我可以用
响应
做什么

如何让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);
您可以使用使模块中的代码意识到
response
是在全局范围内的某个地方定义的:

import { HttpResponse } from './response';

declare global {
    let response: HttpResponse;
}
您可以使用使模块中的代码意识到
response
是在全局范围内的某个地方定义的:

import { HttpResponse } from './response';

declare global {
    let response: HttpResponse;
}