Categories
TypeScript Answers

How to add types of Axios mock using Jest and TypeScript?

Sometimes, we want to add types of Axios mock using Jest and TypeScript.

In this article, we’ll look at how to add types of Axios mock using Jest and TypeScript.

How to add types of Axios mock using Jest and TypeScript?

To add types of Axios mock using Jest and TypeScript, we add the jest.Mocked<typeof axios> type.

For instance, we write

import axios from "axios";
jest.mock("axios");
const mockedAxios = axios as jest.Mocked<typeof axios>;

to get the type of axios with typeof axios.

Then we get the type for the mocked version of axios by wrapping jest.Mocked<> around it.

Conclusion

To add types of Axios mock using Jest and TypeScript, we add the jest.Mocked<typeof axios> type.

Categories
TypeScript Answers

How to add left padding to a string with zeros with TypeScript?

Sometimes, we want to add left padding to a string with zeros with TypeScript.

In this article, we’ll look at how to add left padding to a string with zeros with TypeScript.

How to add left padding to a string with zeros with TypeScript?

To add left padding to a string with zeros with TypeScript, we can use the string padStart method.

For instance, we write

const s: string = str.padStart(9, "0");

to prepend the str string with 0’s until the length of it is 9.

Conclusion

To add left padding to a string with zeros with TypeScript, we can use the string padStart method.

Categories
TypeScript Answers

How to make a variable final in TypeScript?

Sometimes, we want to make a variable final in TypeScript.

In this article, we’ll look at how to make a variable final in TypeScript.

How to make a variable final in TypeScript?

To make a variable final in TypeScript, we can use the readonly keyword.

For instance, we write

class Foo {
  readonly bar = 1; 
  readonly baz: string;
  constructor() {
    this.baz = "hello"; 
  }
}

to make the bar and baz variables readonly.

This means we can’t assign an new value to the variables after they’ve been assigned a value.

Conclusion

To make a variable final in TypeScript, we can use the readonly keyword.

Categories
TypeScript Answers

How to allow an optional parameter to be null in TypeScript?

Sometimes, we want to allow an optional parameter to be null in TypeScript.

In this article, we’ll look at how to allow an optional parameter to be null in TypeScript.

How to allow an optional parameter to be null in TypeScript?

To allow an optional parameter to be null in TypeScript, we can add the null type in a union with other types.

For instance, we write

const foo = (bar: string | null) => {
  console.info(bar);
};

foo("Hello World!"); 
foo(null);

to let us call foo with the bar parameter set to a string or null with the string | null union type.

Then we call foo as we do above without any errors.

Conclusion

To allow an optional parameter to be null in TypeScript, we can add the null type in a union with other types.

Categories
TypeScript Answers

How to await a list of promises in JavaScript or TypeScript?

Sometimes, we want to await a list of promises in JavaScript or TypeScript.

In this article, we’ll look at how to await a list of promises in JavaScript or TypeScript.

How to await a list of promises in JavaScript or TypeScript?

To await a list of promises in JavaScript or TypeScript, we call Promise.all with an array of promises.

For instance, we write

const bar = await Promise.all(promiseArray);

in an async function to call Promise.all with a promiseArray which is an array of promises.

Then we use await to return an array of the resolve value of all the promises and assign it to bar.

Conclusion

To await a list of promises in JavaScript or TypeScript, we call Promise.all with an array of promises.