Skip to content

Conversation

@mrafnadeem-apimatic
Copy link
Contributor

@mrafnadeem-apimatic mrafnadeem-apimatic commented Jun 4, 2025

Closes #236

What Changed

  • Enabled strict mode and forceConsistentCasingInFileNames. Disabled useUnknownInCatchVariables.
  • Fixed type errors due to strict: true, mainly in the schema package.
  • Added tests for numberEnum.
  • Suppressed type errors elsewhere, mostly in tests

Problems

Some compromises had to be made to enable this without it taking weeks of time.

unknownInCatchVariables is set to false

When useUnknownInCatchVariables is disabled, the thrown error in the catch block is type any. If you use unknown in catch variables, you might be tempted to use instanceof in order to narrow it to an Error type. But this check returns false if you threw a class that inherits from Error. Why?

This is because we currently target ES5 in the tsconfig.json file instead of ES6/ES2015.

Keeping it this way is dangerous for the codebase because it is easy to unintentionally introduce a bug in an effort to keep the code type-safe, since TypeScript won't warn us about this. We should upgrade to ES6.

References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#custom_error_types
https://github.com/microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work

In some places, @ts-ignore and any are used

This was done in order to make migrating to strict: true easier. The point of strict: true was to enforce strict type checking on all new code in the codebase. The benefit of strict type safety for all future code is worth the risk.

In some tests, @ts-expect-error is used

This was intentional. It's a problem with using TypeScript with Jest. Using Jest expect functions does not narrow down the type. Ideally, there'd be assertion functions we could use to assert the type but it's not available yet.

When useUnknownInCatchVariables is disabled, the thrown error in the catch block is type any. If you use unknown in catch variables, you might be tempted to use `instanceof` in order to narrow it to an Error type. But this check returns false if you threw a class that inherits from Error. Why?

This is because we currently target ES5 in the `tsconfig.json` file instead of ES6/ES2015.

Keeping it this way is dangerous for the codebase because it is easy to unintentionally introduce a bug in an effort to keep the code type-safe, since TypeScript won't warn us about this.

References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#custom_error_types
https://github.com/microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jun 4, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
5.6% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@mrafnadeem-apimatic mrafnadeem-apimatic removed the request for review from thehappybug June 4, 2025 18:28
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
64.9% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable strict type checking in tsconfig

2 participants