Initializers are not allowed in ambient contexts error when installing Blueprint
Asked Answered
C

10

28

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:

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?

Channing answered 17/2, 2017 at 15:7 Comment(0)
C
24

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.

So before, a line of the emitted classes.d.ts looked like this:

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.

Channing answered 17/2, 2017 at 15:7 Comment(2)
I used this answer after Cjo's and A.T.'s solutions and installed TypeScript 3.1 (upgrade from 3.0), only then the errors disappeared. – Viceregal
Upgrading TypeScript worked for me as well πŸ‘ Was trying to get this Microsoft demo application for Teams up and running, and this was the key. – Linoel
F
15

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.

Florri answered 17/12, 2019 at 3:48 Comment(0)
M
6

I had this problem, but for me, updating the local (and global) TypeScript packages did not resolve the problem. Luckily I came across the following article Which version of TypeScript is Visual Studio Using?

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.

Masry answered 14/5, 2017 at 12:5 Comment(0)
S
2

For windows, to resolve this issue the shortest path is to install Typescript through installer.

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

Southeastwardly answered 26/5, 2017 at 9:20 Comment(0)
R
2

Delete your node-modules folder and do a clean installation.

Rhynd answered 16/6, 2017 at 11:26 Comment(0)
M
2

I had this same issue with a different library than blueprint.js that caused the ts compiler to throw "Initializers are not allowed in ambient contexts" with an index.d.ts file that looks like this:

declare var identikon_cljs = require('identikon_cljs');

And I was able to get it to compile after adding or setting these properties to true in tsconfig.json:

{
    "compilerOptions": {
        "allowJs": true,
        "allowSyntheticDefaultImports": true,
        "skipLibCheck": true
    }
}
Masao answered 21/5, 2021 at 22:28 Comment(1)
The only option I needed is - "skipLibCheck": true. It stops Typescript checking 3rd party libraries in node_modules. – Correy
O
1

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.

Outshout answered 6/3, 2019 at 12:16 Comment(0)
T
-1

Upgrade typescript to at least 3.1+.

Tressietressure answered 23/4, 2019 at 18:58 Comment(1)
I have upgraded to 3.2. but its failed again – Grassquit
F
-2

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)
Felipe answered 25/1, 2019 at 19:10 Comment(0)
V
-4

first update package.json with typescript version:3.6.4 and execute command npm i

Verdugo answered 14/10, 2019 at 15:18 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.