Categories
TypeScript Answers

How to create a strongly typed array of arrays in TypeScript?

Sometimes, we want to create a strongly typed array of arrays in TypeScript.

In this article, we’ll look at how to create a strongly typed array of arrays in TypeScript.

How to create a strongly typed array of arrays in TypeScript?

To create a strongly typed array of arrays in TypeScript, we can nest the Array type.

For instance, we write

const x: Array<Array<Foo>> = [];

to declare variable x with the nested array entries having the Foo type with Array<Array<Foo>>.

Conclusion

To create a strongly typed array of arrays in TypeScript, we can nest the Array type.

Categories
TypeScript Answers

How to add types to class constructor in TypeScript?

Sometimes, we want to add types to class constructor in TypeScript.

In this article, we’ll look at how to add types to class constructor in TypeScript.

How to add types to class constructor in TypeScript?

To add types to class constructor in TypeScript, we can use the typeof operator to get the type from the class.

For instance, we write

class Zoo {
  constructor(public AnimalClass: typeof Animal) {
    const a = new AnimalClass();
  }
}

to use typeof Animal to return the type for the Animal class automatically.

Then we can assume that AnimalClass is the Animal class and instantiate it in the constructor.

Conclusion

To add types to class constructor in TypeScript, we can use the typeof operator to get the type from the class.

Categories
TypeScript Answers

How to add functions to an enum with TypeScript?

Sometimes, we want to add functions to an enum with TypeScript.

In this article, we’ll look at how to add functions to an enum with TypeScript.

How to add functions to an enum with TypeScript?

To add functions to an enum with TypeScript, we can create a class that has an enum has its instance variable.

For instance, we write

enum Mode {
  landscape,
  portrait,
}

class Device {
  constructor(public mode: Mode) {
    console.log(Mode[this.mode]);
  }
}

to add the Device class that takes a Mode variable as the argument of its constructor.

Then we can use Mode[this.mode] to get a value dynamically from the Mode enum.

Conclusion

To add functions to an enum with TypeScript, we can create a class that has an enum has its instance variable.

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.