What are “Ambient Typings” in the Typescript Typings tool?

后端 未结 2 953
闹比i
闹比i 2020-12-05 17:27

I hear the term \"ambient\" used to describe type definitions downloaded with the typings tool. What does that mean, though?

I can\'t seem to find a straightforward

相关标签:
2条回答
  • 2020-12-05 17:55

    From the TypeScript docs:

    An ambient declaration introduces a variable into a TypeScript scope, but has zero impact on the emitted JavaScript program. Programmers can use ambient declarations to tell the TypeScript compiler that some other component will supply a variable. For example, by default the TypeScript compiler will print an error for uses of undefined variables. To add some of the common variables defined by browsers, a TypeScript programmer can use ambient declarations.

    https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#1.1

    To put it a little more simply. Ambient declarations tell the TypeScript compiler that when the JavaScript is executed, something will exist that the TypeScript compiler can't see right now (Because it isn't TypeScript).

    Imagine if you are writing code that uses jQuery. If you just try to write $() TypeScript will think you are using an undeclared variable $ and will throw an error. Ambient declarations like declare var $ tell the TS compiler that, even though $ isn't visible to the compiler, it will exist when the JS is executed.

    0 讨论(0)
  • 2020-12-05 18:12

    From typings v1.0.0 release, the unclear term ambient has been changed to global.

    You can simply think these type definitions are "global" to the project.

    0 讨论(0)
提交回复
热议问题