Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What means "ambient" in TypeScript

Tags:

typescript

I don't understand what means the word ambient in the following sentence:

A function implementation cannot be declared in an ambient context.

I'm not sure to understand the general meaning of the word, (English isn't my maternal language) and if there is a specific meaning here I don't get it as well.

I've tried to understand in my maternal language but couldn't get it in this context. It's something like current context I'd say but it doesn't work out.

The message appeared because I was trying to declare a class, which cannot be declared, only module can. I've fixed it but still don't understand the meaning of the error message here.

like image 211
Vadorequest Avatar asked Nov 15 '14 13:11

Vadorequest


People also ask

What is ambient in TypeScript?

In Typescript, Ambient in refers to the declarations used to inform a compiler that the actual piece of code exists in a different place. For example, a developer might want to write a third-party library in plain JS using jQuery or Angular.

Is it possible to generate TypeScript declaration files from JS library?

With TypeScript 3.7, TypeScript added support for generating . d. ts files from JavaScript using JSDoc syntax. This set up means you can own the editor experience of TypeScript-powered editors without porting your project to TypeScript, or having to maintain .


1 Answers

The english word

Ambience : the character and atmosphere of a place..

TypeScript version

TypeScript declaration files exist to tell the compiler of the environment in which it is running. Hence the word ambient context. You can only do declarations in a declaration context and not implementations.

E.g. if you have some awesomeLibrary declared in a raw JS file that TypeScript does not know about the following will error:

awesomeLibrary = 123; // Error: `awesomeLibrary` is not defined 

So you can declare it in an ambient context and now TypeScript will be fine:

declare var awesomeLibrary: any; awesomeLibrary = 123; // allowed 

More

More on ambient declarations.

like image 50
basarat Avatar answered Oct 06 '22 18:10

basarat