Categories
TypeScript Answers

How to check multiple arguments on multiple calls for Jest spies?

Sometimes, we want to check multiple arguments on multiple calls for Jest spies.

In this article, we’ll look at how to check multiple arguments on multiple calls for Jest spies.

How to check multiple arguments on multiple calls for Jest spies?

To check multiple arguments on multiple calls for Jest spies, we can use the toEqual method with an array.

For instance, we write

expect(mockFn.mock.calls).toEqual([
  [arg1, arg2],
  [arg1, arg2],
]);

to get all the calls of mockFn with mockFn.mock.calls.

And then we check the arguments of the calls by calling toEqual with an array of argument arrays to check the calls against the entries in the arrays.

Conclusion

To check multiple arguments on multiple calls for Jest spies, we can use the toEqual method with an array.

Categories
TypeScript Answers

How to extend the Error class with TypeScript?

To extend the Error class with TypeScript, we can use the extend keyword.

For instance, we write

class FooError extends Error {
  constructor(m: string) {
    super(m);
  }

  sayHello() {
    return this.message;
  }
}

to create the FooError class that inherits from the Error class by adding extends Error.

In the constructor, we call super parent constructor with the arguments required by Error.

And then we add the sayHello method that’s exclusive to the FooError class.

Conclusion

To extend the Error class with TypeScript, we can use the extend keyword.

Categories
TypeScript Answers

How to convert a boolean a to string value with TypeScript?

Sometimes, we want to convert a boolean a to string value with TypeScript.

In this article, we’ll look at how to convert a boolean a to string value with TypeScript.

How to convert a boolean a to string value with TypeScript?

To convert a boolean a to string value with TypeScript, we can use the String function.

For instance, we write

const myBool: boolean = true;
const myString: string = String(myBool);

to call String with myBool to return myBool converted to a string.

Conclusion

To convert a boolean a to string value with TypeScript, we can use the String function.

Categories
TypeScript Answers

How to use enum as a restricted key type in TypeScript?

Sometimes, we want to use enum as a restricted key type in TypeScript.

In this article, we’ll look at how to use enum as a restricted key type in TypeScript.

How to use enum as a restricted key type in TypeScript?

To use enum as a restricted key type in TypeScript, we can use the in keyword.

For instance, we write

enum MyEnum {
  First,
  Second,
}

let layer: { [key in MyEnum]: any };

to create the { [key in MyEnum]: any } type which restricts the keys of the object with the given type to the keys in MyEnum.

We can also make the keys optional with

let layer: { [key in MyEnum]?: any };

We put a ? after [key in MyEnum] to make the keys optional.

Conclusion

To use enum as a restricted key type in TypeScript, we can use the in keyword.

Categories
TypeScript Answers

How to import moment.js with TypeScript?

Sometimes, we want to import moment.js with TypeScript.

In this article, we’ll look at how to import moment.js with TypeScript.

How to import moment.js with TypeScript?

To import moment.js with TypeScript, we can import the whole package with import.

To import moment.js, we write

import * as moment from "moment";

to import the 'moment' module as moment.

Conclusion

To import moment.js with TypeScript, we can import the whole package with import.