我不明白ambient下面这句话的意思是什么:
无法在环境上下文中声明函数实现.
我不太明白这个词的一般含义,(英语不是我的母语),如果这里有特定含义,我也不理解.
我试图用我的母语来理解,但在这种情况下却无法理解.这就像current context我说的那样,但它没有成功.
出现这个消息是因为我正在尝试declare一个无法声明的类,只能module这样做.我已修复它但仍然不理解错误消息的含义.
bas*_*rat 19
气氛:the character and atmosphere of a place..存在TypeScript声明文件以告诉编译器它正在运行的环境.因此,环境语境一词.你只能做声明的声明上下文,而不是实现.
PS:这里有环境声明.
for*_*d04 11
Ambient 的意思是“没有实现”。
环境声明仅存在于类型系统中并在运行时被擦除:
// ambient module declaration
declare module "mymod" { /*... */ }
// ambient namespace declaration
declare namespace MyNamespace { /*... */ }
// ambient variable declaration
declare const myVar: string;
Run Code Online (Sandbox Code Playgroud)
例如declare const myVar: string,就像对编译器的承诺:“假设将在运行时定义一个const myVarwith 类型”(其他情况类似)。string
您也可以将环境视为declareTS 中的关键字。根据定义,所有类型声明(如接口或类型别名)都是隐式环境的,因为编译器很清楚,它们没有运行时影响。
declare type MyType = {a: string} // is valid
type MyType = {a: string} // shorter, so just leave "declare" out
Run Code Online (Sandbox Code Playgroud)
“不能在环境上下文中声明函数实现。”
如前所述,环境声明不能包含运行时代码,例如:
declare module "mymod" {
function foo() { // error: An implementation cannot be declared in ambient contexts.
console.log("bar")
}
}
Run Code Online (Sandbox Code Playgroud)
鉴于"mymod"是一个 npm 包,实现代码宁愿在 下的主.js文件中"node_modules/mymod",而上述类型驻留在单独的.d.ts文件中。
| 归档时间: |
|
| 查看次数: |
19215 次 |
| 最近记录: |