Declaring multiple TypeScript variables with the same type

后端 未结 11 1311
半阙折子戏
半阙折子戏 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 01:03

    let [string1, string2]: string[] = [];
    

    breaking it down:

    • on the left the whole [string1, string2] is declared as string[], implying string1, string2 is of type string
    • to do a destructure you need it to be assigned to something, here empty array []
    • on right hand the empty array is [undefined, undefined, undefined, ...] when destructuring string1 and string2 gets assigned to undefined
    • finally string1, string2 are of type string with value undefined

提交回复
热议问题