[TypeScript] Class Property Inference from Constructors V4

This major convenience feature reduces the need for class field type annotations by inferring their types from assignments in the constructor. It’s important to remember that this only works when noImplicitAny is set to true.

class Color {
  red  // :number no longer needed!
  green // :number no longer needed!
  blue // :number no longer needed!
  constructor(c: [number, number, number]) {
    this.red = c[0]
    this.green = c[1]
    this.blue = c[2]
  }
}

 

posted @ 2022-07-12 14:04  Zhentiw  阅读(26)  评论(0编辑  收藏  举报