TS2322: Type 'BN' is not assignable to type 'string | number | BN' in Docker build

Viewed 40

I have a typescript project that compiles on my local machine successfully, and which I have successfully built as a Docker image locally and via a pipeline on a repository. But when a colleague tries to build it on his machine this esoteric error comes out in a number of files and prevents a build:

error TS2322: Type 'BN' is not assignable to type 'string | number | BN'

BN is referring to the bn.js library.

I am aware that this post does not likely have sufficient information for anyone to definitively solve the question, but what I am looking for are ideas about what may have gone wrong here.

Have you ever seen an instance of a type not being assignable to a type which explicitly includes it? If so, what was the underlying issue?

1 Answers

The root of the issue turned out be NPM, which was the package manager for the project.

The project itself was a lerna monorepo with two packages, both of which relied on BN.js, and where one of the packages imported some classes from the other. However, each package had its own version of BN.js and when different versions of BN.js began to mingle in a single package, weird things began happening.

Without going into many details, the solution was removing BN.js from both packages and instead installing a specific version at the project root level.

Related