Categories
TypeScript Answers

How to declare multiple TypeScript variables with the same type?

Spread the love

Sometimes, we want to declare multiple TypeScript variables with the same type.

In this article, we’ll look at how to declare multiple TypeScript variables with the same type.

How to declare multiple TypeScript variables with the same type?

To declare multiple TypeScript variables with the same type, we can use array destructuring.

For instance, we write

const [x, y]: number[] = [1, 2];

to declare the x and y variables and make them both numbers by setting the array’s type to number[].

Then the TypeScript compiler will infer that both variables’ types are number.

Conclusion

To declare multiple TypeScript variables with the same type, we can use array destructuring.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *