Declaring multiple TypeScript variables with the same type

How about this? Using array deconstruction with Typescript's array type.

let [x,y]: number[]

But please note that this feature is unsafe if you do not turn on pedantic index signature check. For example, the following code will not have compile error even though it should:

let [x, y]: number[] = [1]
console.log(x.toString()) // No problem
console.log(y.toString()) // No compile error, but boom during runtime 

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


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.

Tags:

Typescript