“环境"是什么意思?在打字稿中 [英] What means "ambient" in TypeScript

查看:41
本文介绍了“环境"是什么意思?在打字稿中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白下面句子中ambient这个词是什么意思:

<块引用>

不能在环境上下文中声明函数实现.

我不确定这个词的一般含义,(英语不是我的母语),如果这里有特定的含义,我也不明白.

我试图用我的母语来理解,但在这种情况下无法理解.它类似于 current context 我会说,但它不起作用.

出现该消息是因为我试图声明一个不能声明的类,只有module可以.我已经修复了它,但仍然不明白这里错误消息的含义.

解决方案

Ambient 仅仅意味着 "没有实现".

环境声明只存在于类型系统中并在运行时被删除:

//环境模块声明声明模块mymod"{/*... */}//环境命名空间声明声明命名空间 MyNamespace {/*... */}//环境变量声明声明 const myVar: 字符串;

例如 declare const myVar: string 就像是对编译器的承诺:假设会有一个 const myVar 类型为 string> 在运行时定义"(其他情况类似).

您也可以将环境视为 TS 中的 declare 关键字.所有类型声明,如 interfaces类型别名 根据定义是隐式环境的,因为编译器很清楚,这些没有运行-时间影响.

declare type MyType = {a: string}//有效type MyType = {a: string}//更短,所以把declare"去掉

<小时><块引用>

不能在环境上下文中声明函数实现."

如前所述,环境声明不能包含运行时代码,例如:

声明模块mymod"{function foo() {//错误:无法在环境上下文中声明实现.console.log("bar")}}

鉴于 "mymod" 是一个 npm 包,实现代码宁愿在 "node_modules/mymod" 下的主要 .js 文件中> 和以上类型驻留在单独的 .d.ts 文件中.

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.

解决方案

Ambient simply means "without implementation".

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

You also can think of ambient as the declare keyword in TS. All type declarations like interfaces or type aliases are implicitly ambient by definition, as it is clear for the compiler, that these have no run-time impact.

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.

这篇关于“环境"是什么意思?在打字稿中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆