typescript 安装蓝图时,环境上下文错误中不允许使用初始化程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42301181/
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
Initializers are not allowed in ambient contexts error when installing Blueprint
提问by JKillian
I'm trying to use the @blueprintjs/core
library in my project. However, when I compile my code, I'm getting many errors like this:
我正在尝试@blueprintjs/core
在我的项目中使用该库。然而,当我编译我的代码时,我收到了很多这样的错误:
node_modules/@blueprintjs/core/dist/common/classes.d.ts(4,30):
error TS1039: Initializers are not allowed in ambient contexts.
What's going on? What am I doing wrong?
这是怎么回事?我究竟做错了什么?
采纳答案by JKillian
As of @blueprintjs/[email protected]
, Blueprint is now compiled using TypeScript 2.1. With this new version of TypeScript, initializers are added to the emitted typings for constants.
从 开始@blueprintjs/[email protected]
,蓝图现在使用 TypeScript 2.1 编译。在这个新版本的 TypeScript 中,初始化器被添加到发出的常量类型中。
So before, a line of the emitted classes.d.ts
looked like this:
所以之前,发出的一行classes.d.ts
看起来像这样:
export declare const ACTIVE: string;
It now looks like this and includes an initializer:
它现在看起来像这样,并包含一个初始化程序:
export declare const ACTIVE = "pt-active";
This new syntax in declaration files makes old versions of the compiler unhappy. To make the error go away, you'll need to make sure you're compiling your project with at least TypeScript 2.1.
声明文件中的这种新语法使旧版本的编译器不满意。为了消除错误,您需要确保至少使用 TypeScript 2.1 编译您的项目。
回答by Neilski
I had this problem, but for me, updating the local (and global) TypeScriptpackages did notresolve the problem. Luckily I came across the following article Which version of TypeScript is Visual Studio Using?
我遇到了这个问题,但对我来说,更新本地(和全局)TypeScript包并没有解决问题。幸运的是,我看到了以下文章Visual Studio 使用哪个版本的 TypeScript?
In a nutshell, whilst I had updated to TypeScript 2.2, Visual Studio was still referencing version 2.0 in the .csproj
file. I hope this helps someone else with a similar problem.
简而言之,虽然我已更新到TypeScript 2.2,但 Visual Studio 仍在.csproj
文件中引用 2.0 版。我希望这可以帮助其他有类似问题的人。
回答by A.T.
For windows, to resolve this issue the shortest path is to install Typescript through installer.
对于windows,解决这个问题的最短路径是通过安装程序安装Typescript。
https://www.microsoft.com/en-us/download/details.aspx?id=48593
https://www.microsoft.com/en-us/download/details.aspx?id=48593
回答by Cjo
Delete your node-modules folder and do a clean installation.
删除您的 node-modules 文件夹并进行全新安装。
回答by Pete Herc
I've been having this issue. Needed to download the latest version of typescript from typescript website and make sure 'use latest version' is selected in visual studio project properties.
我一直有这个问题。需要从打字稿网站下载最新版本的打字稿,并确保在 Visual Studio 项目属性中选择“使用最新版本”。
回答by Ismayil Niftaliyev
Upgrade typescript to at least 3.1+.
将打字稿升级到至少 3.1+。
回答by raphisama
The problem is that you are writing code in a d.ts
file.
You should only be writing types in these "ambient" files. Things like initializing variables, writing functions, etc should not be done in "ambient" files.
问题是您正在d.ts
文件中编写代码。您应该只在这些“环境”文件中写入类型。初始化变量、编写函数等不应该在“环境”文件中完成。
回答by krishna karuturi
first update package.json with typescript version:3.6.4 and execute command npm i
首先使用 typescript version:3.6.4 更新 package.json 并执行命令 npm i
回答by Srikrushna
This error is because of node older version of node
此错误是因为节点较旧版本的节点
You can remove the node version and reinstall it by using the below comand
您可以使用以下命令删除节点版本并重新安装
rm -rf node_modules //For removing
npm install //Install again(Fresh with updated one)