Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring multiple TypeScript variables with the same type

I'm coding a large TypeScript class and I've set noImplicitAny to true. Is there any way to declare multiple variables of the same type on the same line?

I'd like to declare x and y as numbers with something like "x, y: number". But the compiler doesn't like that or anything else I've tried. Is there a better alternative to "x: number; y: number"?

like image 231
MatthewScarpino Avatar asked Dec 11 '15 20:12

MatthewScarpino


People also ask

How do you assign the same value to multiple variables in TypeScript?

Put the varible in an array and Use a for Loop to assign the same value to multiple variables.

Can you declare multiple variables on the same line in JavaScript?

You can declare multiple variables in a single line. However, there are more efficient ways to define numerous variables in JavaScript. First, define the variables' names and values separated by commas using just one variable keyword from var , let or const.

Can you declare more than one variable of the same data type on a line?

Do not declare more than one variable per declaration. Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values.


Video Answer


2 Answers

There is no real way to achieve what you want. If your only goal is to compress everything onto one line, you can do the following:

public AcccountNumber: number;public branchCode:number; 

…but I wouldn't recommend it.

like image 23
Venkatesh Muniyandi Avatar answered Sep 19 '22 18:09

Venkatesh Muniyandi


There isn't any syntax that would accomplish this in a better way than just writing the type twice.

like image 61
Ryan Cavanaugh Avatar answered Sep 21 '22 18:09

Ryan Cavanaugh