【问题标题】:Initializers are not allowed in ambient contexts error when installing Blueprint安装蓝图时,环境上下文中不允许初始化程序错误
【发布时间】:2021-05-22 06:28:05
【问题描述】:

我正在尝试在我的项目中使用 @blueprintjs/core 库。但是,当我编译我的代码时,我遇到了很多这样的错误:

node_modules/@blueprintjs/core/dist/common/classes.d.ts(4,30):
  error TS1039: Initializers are not allowed in ambient contexts.

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

【问题讨论】:

    标签: typescript blueprintjs


    【解决方案1】:

    截至@blueprintjs/core@1.7.0,蓝图现在使用 TypeScript 2.1 编译。在这个新版本的 TypeScript 中,初始化器被添加到为常量发出的类型中。

    所以之前,发出的classes.d.ts 的一行看起来像这样:

    export declare const ACTIVE: string;
    

    它现在看起来像这样并包含一个初始化程序:

    export declare const ACTIVE = "pt-active";
    

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

    【讨论】:

    • 我在 Cjo 和 A.T. 的解决方案之后使用了这个答案并安装了 TypeScript 3.1(从 3.0 升级),然后错误才消失。
    • 升级 TypeScript 也对我有用 ? 试图让 this Microsoft demo application for Teams 启动并运行,这是关键。
    【解决方案2】:

    问题是您在d.ts 文件中编写代码。 您应该只在这些“环境”文件中编写类型。初始化变量、编写函数等不应该在“环境”文件中完成。

    【讨论】:

      【解决方案3】:

      我遇到了这个问题,但对我来说,更新本地(和全局)TypeScript 包并没有 解决问题。幸运的是,我偶然发现了以下文章Which version of TypeScript is Visual Studio Using?

      简而言之,虽然我已更新到 TypeScript 2.2,但 Visual Studio 仍然在 .csproj 文件中引用 2.0 版。我希望这可以帮助遇到类似问题的其他人。

      【讨论】:

        【解决方案4】:

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

        https://www.microsoft.com/en-us/download/details.aspx?id=48593

        【讨论】:

          【解决方案5】:

          删除您的 node-modules 文件夹并进行全新安装。

          【讨论】:

            【解决方案6】:

            我在使用与 blueprint.js 不同的库时遇到了同样的问题,导致 ts 编译器抛出“环境上下文中不允许初始化程序”,index.d.ts 文件如下所示:

            declare var identikon_cljs = require('identikon_cljs');
            

            在 tsconfig.json 中添加或设置这些属性为 true 后,我能够编译它:

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

            【讨论】:

            • 我需要的唯一选项是 - “skipLibCheck”:true。它会停止 Typescript 检查 node_modules 中的第 3 方库。
            【解决方案7】:

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

            【讨论】:

              【解决方案8】:

              将 typescript 升级到至少 3.1+。

              【讨论】:

              • 我已经升级到 3.2。但它又失败了
              【解决方案9】:

              这个错误是由于节点的旧版本节点

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

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

              【讨论】:

                【解决方案10】:

                首先使用 typescript version:3.6.4 更新 package.json 并执行命令 npm i

                【讨论】:

                  猜你喜欢
                  • 2017-09-05
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-04-29
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-02-01
                  相关资源
                  最近更新 更多