21, Feb 2023
Why ‘X’ + Y = ‘XY’ in TypeScript ? Expect an error

In TypeScript, when a string is concatenated with a non-string value using the “+” operator, the non-string value is implicitly converted to a string before concatenation. This is known as type coercion. This is why the expression ‘X’ + Y results in ‘XY’ instead of an error.

For example, in the following code snippet, the variable Y is of type number, but it gets implicitly converted to a string before getting concatenated with ‘X’:

let Y = 10;
let result = 'X' + Y;
console.log(result); // Output: 'X10

This behavior is not specific to TypeScript, but it’s common in many programming languages. This is because the + operator is overloaded in many languages, it can be used for both mathematical addition and string concatenation. If one operand is a string, the other is converted to a string, then concatenation is applied.

However, if you want to avoid this behavior, you can use a more explicit way of converting a non-string value to a string, such as using the String() function or the template literals \ `.

let Y = 10;
let result = 'X' + String(Y);
console.log(result); // Output: 'X10'

let result2 = `X${Y}`;
console.log(result2); // Output: 'X10'

In this way, you can avoid unexpected behavior, and also make your code more readable.

21, Feb 2023
Error TS2351: This expression is not constructable. (AJV with TypeScript)

The “Error TS2351: This expression is not constructable” error in TypeScript is typically caused when you are trying to use a class that is not meant to be instantiated.

Here are a couple of things you can try to fix this error:

  1. Make sure that the class you are trying to instantiate is not abstract. Abstract classes cannot be instantiated and must be extended by a derived class.
  2. Make sure that the class you are trying to instantiate is not an interface. Interfaces cannot be instantiated and must be implemented by a class.
  3. Make sure that the class you are trying to instantiate is exported correctly. You should be able to import the class and then create an instance of it.
  4. Make sure that the class you are trying to instantiate is not a type alias. Type aliases cannot be instantiated and must be used as a type.
  5. Make sure that the class you are trying to instantiate is not a namespace. Namespaces cannot be instantiated.
  6. Make sure that the class you are trying to instantiate is in the correct scope, if the class is in a module, you may need to import it.
  7. Make sure that you are using the correct syntax to instantiate the class. In TypeScript, classes must be instantiated with the new keyword.
  8. Make sure that the class is being imported from the correct location.
  9. If you’re using a library like AJV, make sure that the class is being imported from the correct module.
  10. Check if there’s any other error in the file or in the project that is blocking the instantiation of the class.

These are some general suggestions, the actual cause of the error may vary depending on the specific circumstances. It would be helpful to check the code around the line where the error is thrown, this will give more information about the problem.