Typescript 安装Blueprint时,环境上下文中不允许使用初始值设定项错误

Typescript 安装Blueprint时,环境上下文中不允许使用初始值设定项错误,typescript,blueprintjs,Typescript,Blueprintjs,我试图在我的项目中使用@blueprintjs/core库。然而,当我编译代码时,我会遇到很多类似这样的错误: node_modules/@blueprintjs/core/dist/common/classes.d.ts(4,30): error TS1039: Initializers are not allowed in ambient contexts. export declare const ACTIVE: string; declare var identikon_cljs

我试图在我的项目中使用
@blueprintjs/core
库。然而,当我编译代码时,我会遇到很多类似这样的错误:

node_modules/@blueprintjs/core/dist/common/classes.d.ts(4,30):
  error TS1039: Initializers are not allowed in ambient contexts.
export declare const ACTIVE: string;
declare var identikon_cljs = require('identikon_cljs');

发生什么事了?我做错了什么?

@blueprintjs开始/core@1.7.0
,Blueprint现在使用TypeScript 2.1编译。在这个新版本的TypeScript中,初始值设定项被添加到所发出的常量类型中

所以之前,一行发出的
classes.d.ts
如下所示:

node_modules/@blueprintjs/core/dist/common/classes.d.ts(4,30):
  error TS1039: Initializers are not allowed in ambient contexts.
export declare const ACTIVE: string;
declare var identikon_cljs = require('identikon_cljs');
它现在看起来像这样,包括一个初始值设定项:

export declare const ACTIVE = "pt-active";

声明文件中的这种新语法使旧版本的编译器不满意要消除错误,您需要确保编译项目时至少使用了TypeScript 2.1。

我遇到了这个问题,但对我来说,更新本地(和全局)TypeScript包并不能解决问题。幸运的是,我看到了下面的文章


简而言之,当我更新到TypeScript 2.2时,VisualStudio仍然在
.csproj
文件中引用版本2.0。我希望这能帮助其他人解决类似问题。

对于windows,要解决此问题,最短的路径是通过安装程序安装Typescript


删除节点模块文件夹并进行干净的安装

此错误是由于节点的旧版本导致的

您可以使用以下命令删除节点版本并重新安装

rm -rf node_modules //For removing
npm install //Install again(Fresh with updated one)

我一直有这个问题。需要从typescript网站下载最新版本的typescript,并确保在visual studio项目属性中选择了“使用最新版本”。

将typescript升级到至少3.1+。

使用typescript版本:3.6.4首次更新package.json并执行命令npm i

问题是您正在使用
d.ts
文件。
您应该只在这些“环境”文件中写入类型。初始化变量、编写函数等操作不应在“环境”文件中进行。

我在blueprint.js之外的另一个库中遇到同样的问题,导致ts编译器抛出“环境上下文中不允许使用初始值设定项”的
index.d.ts
文件如下所示:

node_modules/@blueprintjs/core/dist/common/classes.d.ts(4,30):
  error TS1039: Initializers are not allowed in ambient contexts.
export declare const ACTIVE: string;
declare var identikon_cljs = require('identikon_cljs');
在tsconfig.json中添加这些属性或将其设置为true之后,我能够编译它:

{
    "compilerOptions": {
        "allowJs": true,
        "allowSyntheticDefaultImports": true,
        "skipLibCheck": true
    }
}

在Cjo和A.T.的解决方案和安装TypeScript 3.1(从3.0升级)之后,我使用了这个答案,直到错误消失。升级TypeScript对我也很有效,我已经升级到3.2。但它又失败了