Categories
TypeScript Answers

How to take object out of array based on attribute value with TypeScript?

Sometimes, we want to take object out of array based on attribute value with TypeScript.

In this article, we’ll look at how to take object out of array based on attribute value with TypeScript.

How to take object out of array based on attribute value with TypeScript?

To take object out of array based on attribute value with TypeScript, we can use the array find method.

For instance, we write

const array = [
  { id: 1, value: "itemname" },
  { id: 2, value: "itemname" },
];

const item1 = array.find((i) => i.id === 1);

to get the item with id 1 from array and return it with

array.find((i) => i.id === 1)

We call find with a callback that returns the condition of the item we’re looking for in array.

Conclusion

To take object out of array based on attribute value with TypeScript, we can use the array find method.

Categories
TypeScript Answers

How to access static methods in TypeScript?

Sometimes, we want to access static methods in TypeScript.

In this article, we’ll look at how to access static methods in TypeScript.

How to access static methods in TypeScript?

To access static methods in TypeScript, we just access them directly from the class.

For instance, if we have

class Logger {
  static Log() {
    /// ...
  }
}

Then we can call the Log method by writing

Logger.Log();

Conclusion

To access static methods in TypeScript, we just access them directly from the class.

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.