Declaring multiple TypeScript variables with the same type

后端 未结 11 1314
半阙折子戏
半阙折子戏 2020-12-10 00:09

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 lik

11条回答
  •  一整个雨季
    2020-12-10 00:43

    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 
    

提交回复
热议问题