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"?
Put the varible in an array and Use a for Loop to assign the same value to multiple variables.
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.
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.
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.
There isn't any syntax that would accomplish this in a better way than just writing the type twice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With