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.