Categories
TypeScript Answers

How to break a forEach loop in TypeScript?

Sometimes, we want to break a forEach loop in TypeScript.

In this article, we’ll look at how to break a forEach loop in TypeScript.

How to break a forEach loop in TypeScript?

To break a forEach loop in TypeScript, we replace forEach with a for-of loop.

For instance, we write

for (const a of this.tab.committee.ratings) {
  if (somethingWrong) {
    break;
  }
}

to loop through the this.tab.committee.ratings iterable object with a for-of loop.

And if somethingWrong is true, then we use break to stop the loop.

Conclusion

To break a forEach loop in TypeScript, we replace forEach with a for-of loop.

Categories
TypeScript Answers

How to declare a delegate type in TypeScript?

Sometimes, we want to declare a delegate type in TypeScript.

In this article, we’ll look at how to declare a delegate type in TypeScript.

How to declare a delegate type in TypeScript?

To declare a delegate type in TypeScript, we can define an interface that restricts the signature and return type of a function.

For instance, we write

interface Greeter {
  (message: string): void;
}

const sayHi = (greeter: Greeter) => {
  greeter("Hello!");
};

sayHi((msg) => console.log(msg));

to define the Greeter interface that has signature (message: string) and has the void return type.

Then we set the greeter parameter of the sayHi function to the Greeter type.

Therefore, greeter takes a string and returns nothing.

Conclusion

To declare a delegate type in TypeScript, we can define an interface that restricts the signature and return type of a function.

Categories
TypeScript Answers

How to check if specific object is empty in TypeScript?

Sometimes, we want to check if specific object is empty in TypeScript.

In this article, we’ll look at how to check if specific object is empty in TypeScript.

How to check if specific object is empty in TypeScript?

To check if specific object is empty in TypeScript, we can use the Object.keys method.

For instance, we write

const isEmpty = Object.keys(obj).length === 0;

to use Object.keys to return an array of non-inherited keys in obj.

Then we get the length of that and check if it’s 0 to see if obj is empty or not.

Conclusion

To check if specific object is empty in TypeScript, we can use the Object.keys method.

Categories
TypeScript Answers

How to fix the error ‘”TS2322: Type ‘Timeout’ is not assignable to type ‘number'” when running unit tests with TypeScript?

Sometimes, we want to fix the error ‘"TS2322: Type ‘Timeout’ is not assignable to type ‘number’" when running unit tests with TypeScript.

In this article, we’ll look at how to fix the error ‘"TS2322: Type ‘Timeout’ is not assignable to type ‘number’" when running unit tests with TypeScript.

How to fix the error ‘"TS2322: Type ‘Timeout’ is not assignable to type ‘number’" when running unit tests with TypeScript?

To fix the error ‘"TS2322: Type ‘Timeout’ is not assignable to type ‘number’" when running unit tests’ with TypeScript, we can define the type of the value returned by setTimeout.

For instance, we write

let timeoutId: null | ReturnType<typeof setTimeout> = null;

timeoutId = setTimeout(() => {
  //...
});

to set the timeoutId to the null | ReturnType<typeof setTimeout> union type.

We use typeof setTimeout to get the type for the setTimeout function.

And we use ReturnType to get the return type of the setTimeout function.

Then we can assign the value returned by setTimeout to timeoutId without error.

Conclusion

To fix the error ‘"TS2322: Type ‘Timeout’ is not assignable to type ‘number’" when running unit tests’ with TypeScript, we can define the type of the value returned by setTimeout.

Categories
TypeScript Answers

How to import a JavaScript library without definition file in a TypeScript file?

Sometimes, we want to import a JavaScript library without definition file in a TypeScript file.

In this article, we’ll look at how to import a JavaScript library without definition file in a TypeScript file.

How to import a JavaScript library without definition file in a TypeScript file?

To import a JavaScript library without definition file in a TypeScript file, we can add the typed definition file for the module in our own project.

To do this, we add

declare module "lib/errorInfoHandler" {
  const noTypeInfoYet: any;
  export = noTypeInfoYet;
}

into a .d.ts file in our TypeScript project.

We add our own type definition for the lib/errorInfoHandler module.

In it, we add the members of the modules that we know exit with

const noTypeInfoYet: any;
export = noTypeInfoYet;

Conclusion

To import a JavaScript library without definition file in a TypeScript file, we can add the typed definition file for the module in our own project.