TypeScript static classes

后端 未结 12 693
太阳男子
太阳男子 2020-12-04 13:34

I wanted to move to TypeScript from traditional JS because I like the C#-like syntax. My problem is that I can\'t find out how to declare static classes in TypeScript.

12条回答
  •  时光说笑
    2020-12-04 14:20

    Defining static properties and methods of a class is described in 8.2.1 of the Typescript Language Specification:

    class Point { 
      constructor(public x: number, public y: number) { } 
      public distance(p: Point) { 
        var dx = this.x - p.x; 
        var dy = this.y - p.y; 
        return Math.sqrt(dx * dx + dy * dy); 
      } 
      static origin = new Point(0, 0); 
      static distance(p1: Point, p2: Point) { 
        return p1.distance(p2); 
      } 
    }
    

    where Point.distance() is a static (or "class") method.

提交回复
热议问题