Renato
Renato

Reputation: 13710

How to declare pre-existing variable in a TypeScript script?

I have a script which will run with a pre-defined binding, say response.

I have a TS interface which informs my code of what I can do with response.

How can I let TypeScript know that this pre-defined variable exists?

This is what I want to achieve:

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);

Upvotes: 0

Views: 326

Answers (1)

artem
artem

Reputation: 51769

You can use global scope augmentation to make the code in the module aware that response is defined somewhere in the global scope:

import { HttpResponse } from './response';

declare global {
    let response: HttpResponse;
}

Upvotes: 3

Related Questions