TypeScript 中的“环境”是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26946495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What means "ambient" in TypeScript
提问by Vadorequest
I don't understand what means the word ambient
in the following sentence:
我不明白ambient
以下句子中的单词是什么意思:
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.
我试图用我的母语来理解,但在这种情况下无法理解。这就像current context
我说的,但它不起作用。
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.
出现该消息是因为我正在尝试declare
一个不能声明的类,只能声明module
。我已经修复了它,但仍然不明白这里错误消息的含义。
采纳答案by ford04
Ambient simply means "without implementation".
Ambient 的意思是“没有实现”。
Ambient declarations only exist in the type system and are erased at run-time:
环境声明仅存在于类型系统中并在运行时被擦除:
// ambient module declaration
declare module "mymod" { /*... */ }
// ambient namespace declaration
declare namespace MyNamespace { /*... */ }
// ambient variable declaration
declare const myVar: string;
For example declare const myVar: string
is like a promise to the compiler: "Assume that there will be a const myVar
with type string
defined at run-time" (other cases analogue).
例如declare const myVar: string
,就像对编译器的承诺:“假设将在运行时定义一个const myVar
with 类型”(其他情况类似)。string
You also can think of ambient as the declare
keyword in TS. All type declarations like interfacesor type aliasesare implicitly ambient by definition, as it is clear for the compiler, that these have no run-time impact.
您也可以将环境视为declare
TS 中的关键字。根据定义,所有类型声明(如接口或类型别名)都是隐式环境的,因为编译器很清楚,它们没有运行时影响。
declare type MyType = {a: string} // is valid
type MyType = {a: string} // shorter, so just leave "declare" out
"A function implementation cannot be declared in an ambient context."
“不能在环境上下文中声明函数实现。”
As said, ambient declarations cannot contain run-time code, like:
如前所述,环境声明不能包含运行时代码,例如:
declare module "mymod" {
function foo() { // error: An implementation cannot be declared in ambient contexts.
console.log("bar")
}
}
Given "mymod"
is a npm package, the implementation code would rather be in the main .js
file under "node_modules/mymod"
, and above types reside in a separate .d.ts
file.
鉴于"mymod"
是一个 npm 包,实现代码宁愿在 下的主.js
文件中"node_modules/mymod"
,而上述类型驻留在单独的.d.ts
文件中。
回答by basarat
The english word
英文单词
Ambience : the character and atmosphere of a place.
.
氛围: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 declarationsin a declaration context and not implementations.
TypeScript 声明文件的存在是为了告诉编译器它运行的环境。因此这个词环境上下文。你只能做声明的声明上下文,而不是实现。
E.g. if you have some awesomeLibrary
declared in a raw JS file that TypeScript does not know about the following will error:
例如,如果您awesomeLibrary
在原始 JS 文件中声明了一些TypeScript 不知道以下内容的声明,则会出错:
awesomeLibrary = 123; // Error: `awesomeLibrary` is not defined
So you can declare it in an ambient context and now TypeScript will be fine:
所以你可以在环境上下文中声明它,现在 TypeScript 就可以了:
declare var awesomeLibrary: any;
awesomeLibrary = 123; // allowed