Categories
TypeScript Answers

How to fix the ‘tsc is not recognized as internal or external command’ error with TypeScript?

Sometimes, we want to fix the ‘tsc is not recognized as internal or external command’ error with TypeScript.

In this article, we’ll look at how to fix the ‘tsc is not recognized as internal or external command’ error with TypeScript.

How to fix the ‘tsc is not recognized as internal or external command’ error with TypeScript?

To fix the ‘tsc is not recognized as internal or external command’ error with TypeScript, we can install the typescript package globally.

To do this, we run

npm install -g typescript 

to install the typescript package globally.

Conclusion

To fix the ‘tsc is not recognized as internal or external command’ error with TypeScript, we can install the typescript package globally.

Categories
TypeScript Answers

How to add custom typings in TypeScript?

Sometimes, we want to add custom typings in TypeScript.

In this article, we’ll look at how to add custom typings in TypeScript.

How to add custom typings in TypeScript?

To add custom typings in TypeScript, we can add declare statements into .d.ts files into our project.

For instance, we write

declare module "some-js-lib" {
  export function hello(world: string): void;
}

to add typings for the some-js-lib module.

We add the hello function declaration into it so that the TypeScript compiler knows that the hello function is available in some-js-lib.

Conclusion

To add custom typings in TypeScript, we can add declare statements into .d.ts files into our project.

Categories
TypeScript Answers

How to create the GUID class with TypeScript?

Sometimes, we want to create the GUID class with TypeScript.

In this article, we’ll look at how to create the GUID class with TypeScript.

How to create the GUID class with TypeScript?

To create the GUID class with TypeScript, we can create a class and add a static method that returns GUID into it.

For instance, we write

class Guid {
  static newGuid() {
    return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
      const r = (Math.random() * 16) | 0;
      const v = c === "x" ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    });
  }
}

to create the Guid class with the newGuid static method.

In it, we have the placeholder string "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" and we call replace on it to replace the placeholders x and y with random values.

We use the bitwise & operator to replace any values that aren’t x.

Then we return the hex string with v.toString(16).

Conclusion

To create the GUID class with TypeScript, we can create a class and add a static method that returns GUID into it.

Categories
TypeScript Answers

How to extend String prototype with TypeScript?

Sometimes, we want to extend String prototype with TypeScript.

In this article, we’ll look at how to extend String prototype with TypeScript.

How to extend String prototype with TypeScript?

To extend String prototype with TypeScript, we can add our entries to the String interface in a .d.ts file in our project.

For instance, we write

declare global {
  interface String {
    padZero(length: number): string;
  }
}

in a .d.ts file to add the padZero method into the String interface.

Then we can add the padZero method into the String prototype without errors by writing

String.prototype.padZero = function (this: string, length: number) {
  const s = this;
  while (s.length < length) {
    s = "0" + s;
  }
  return s;
};

We add this as a parameter of the function and set it to string.

The this parameter will be discarded when the code is compiled into JavaScript.

Conclusion

To extend String prototype with TypeScript, we can add our entries to the String interface in a .d.ts file in our project.

Categories
TypeScript Answers

How to fix the “The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type” error with TypeScript?

Sometimes, we want to fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript.

In this article, we’ll look at how to fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript.

How to fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript?

To fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript, we should make sure all the operands we use with arithmetic operators are numbers.

For instance, if we’re subtracting 2 timestamps, we should make sure they’re both timestamps by calling the getTime method with the Date instances.

So we write

const d = Math.abs(new Date().getTime() - new Date(lastConnect).getTime());

to call getTime with both Date instances to return the timestamps before we subtract them.

Conclusion

To fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript, we should make sure all the operands we use with arithmetic operators are numbers.

For instance, if we’re subtracting 2 timestamps, we should make sure they’re both timestamps by calling the getTime method with the Date instances.