Initializers are not allowed in ambient contexts error when installing Blueprint

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?


Solution 1:

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.

Solution 2:

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.

Solution 3:

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.