Coding⏱️ 3 min read📅 2026-06-03

How to Fix: "TS2322: Type 'Timeout' is not assignable to type 'number'" when running unit tests

TS2322 error when running unit tests with npm link

Quick Answer: When using npm link, ensure that the package being linked has a version that matches the one installed in the current directory. This can be achieved by installing the same version of Package B using npm install instead of npm link.

The error 'TS2322: Type 'Timeout' is not assignable to type 'number'' occurs when running unit tests for a TypeScript package that depends on another package. This issue affects developers who use npm link to create a symlink between packages and run unit tests.

This error can be frustrating because it prevents the unit tests from executing, causing delays in development and testing processes. However, by following these steps, you should be able to resolve this issue and get your unit tests running smoothly again.

💡 Why You Are Getting This Error

  • The primary cause of this error is that the setTimeout function returns a timeout ID, which is an object with a number property, but in some cases, it can return null or undefined. When npm link creates a symlink between packages, it modifies the package.json file to include the linked package's dependencies. This modification can sometimes affect the way the package is executed, leading to unexpected behavior.
  • An alternative reason for this error could be related to how Karma executes tests. If Karma is not configured correctly, it might cause issues with the setTimeout function, resulting in the error TS2322.

🔧 Proven Troubleshooting Steps

Resolving the issue by modifying package.json

  1. Step 1: Open your package A's package.json file and add a script to run the unit tests. For example: "test": "karma".
  2. Step 2: Modify the karma configuration to handle the setTimeout function correctly. You can do this by adding the following line in your karma.conf.js file: "config.set('timeout', 10000)".
  3. Step 3: Save the changes and try running the unit tests again.

Resolving the issue using a different approach

  1. Step 1: Consider using a different testing library that does not rely on setTimeout. For example, you can use Jest or Mocha.
  2. Step 2: If you still want to use Karma, try to isolate the issue by running the tests in a separate process. You can do this by adding the following line in your karma.conf.js file: "config.set('singleRun', false)".
  3. Step 3: This will allow you to run the tests in parallel and avoid the timeout issue.

💡 Conclusion

By following these steps, you should be able to resolve the 'TS2322: Type 'Timeout' is not assignable to type 'number'' error when running unit tests for a TypeScript package that depends on another package. Remember to always keep your dependencies up-to-date and test your code thoroughly to avoid such issues in the future.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions