1 element class
   2     tag class
   3     ast
   4     category
   5     item
   6         title Simple class
   7         expected
   8             + class Greeter {
   9                 + greeting: string;
  10                 + constructor(message: string) {
  11                     + this.greeting = message;
  12                 + }
  13                 + greet() {
  14                     + return "Hello, " + this.greeting;
  15                 + }
  16             + }
  17             + let greeter = new Greeter("world");
  18         ittf
  19             class Greeter
  20                 p greeting
  21                     :string
  22                 ctor
  23                     param message
  24                         :string
  25                     set this.greeting = message
  26                 m greet
  27                     return "Hello, " + this.greeting
  28             let greeter = new Greeter("world")
  29     item
  30         title Class extension
  31         expected
  32             + class Animal {
  33                 + move(distanceInMeters: number = 0) {
  34                     + console.log(`Animal moved ${distanceInMeters}m.`);
  35                 + }
  36             + }
  37             + class Dog extends Animal {
  38                 + bark() {
  39                     + console.log('Woof! Woof!');
  40                 + }
  41             + }
  42         ittf
  43             class Animal
  44                 m move
  45                     param distanceInMeters
  46                         :number
  47                         := 0
  48                     _ console.log
  49                         template
  50                             + Animal moved${}
  51                             @ distanceInMeters
  52                             + m.
  53             class Dog
  54                 super Animal
  55                 m bark
  56                     _ console.log('Woof! Woof!')
  57     item
  58         title Complex class example
  59         expected
  60             + class Animal {
  61                 + name: string;
  62                 + constructor(theName: string) { this.name = theName; }
  63                 + move(distanceInMeters: number = 0) {
  64                     + console.log(`${this.name} moved ${distanceInMeters}m.`);
  65                 + }
  66             + }
  67             + class Snake extends Animal {
  68                 + constructor(name: string) { super(name); }
  69                 + move(distanceInMeters = 5) {
  70                     + console.log("Slithering...");
  71                     + super.move(distanceInMeters);
  72                 + }
  73             + }
  74             + class Horse extends Animal {
  75                 + constructor(name: string) { super(name); }
  76                 + move(distanceInMeters = 45) {
  77                     + console.log("Galloping...");
  78                     + super.move(distanceInMeters);
  79                 + }
  80             + }
  81             + let sam = new Snake("Sammy the Python");
  82             + let tom: Animal = new Horse("Tommy the Palomino");
  83             + sam.move();
  84             + tom.move(34);
  85         ittf
  86             +
  87                 class Animal
  88                     p name
  89                         :string
  90                     ctor
  91                         param theName
  92                             :string
  93                         set this.name = theName
  94                     m move
  95                         param distanceInMeters
  96                             :number
  97                             := 0
  98                         _ console.log
  99                             template
 100                                 +
 101                                 @ this.name
 102                                 + ${}moved${}
 103                                 @ distanceInMeters
 104                                 + m.
 105                 class Snake
 106                     super Animal
 107                     ctor
 108                         param name
 109                             :string
 110                         _ super(name)
 111                     m move
 112                         param distanceInMeters = 5
 113                         _ console.log("Slithering...")
 114                         _ super.move(distanceInMeters)
 115                 class Horse
 116                     super Animal
 117                     ctor
 118                         param name
 119                             :string
 120                         _ super(name)
 121                     m move
 122                         param distanceInMeters = 45
 123                         _ console.log("Galloping...")
 124                         _ super.move(distanceInMeters)
 125     item
 126         title Class accessors
 127         expected
 128             + class Animal {
 129                 + private name: string;
 130                 + constructor(theName: string) { this.name = theName; }
 131             + }
 132             + class Rhino extends Animal {
 133                 + constructor() { super("Rhino"); }
 134             + }
 135             + class Employee {
 136                 + private name: string;
 137                 + constructor(theName: string) { this.name = theName; }
 138             + }
 139             + let animal = new Animal("Goat");
 140             + let rhino = new Rhino();
 141             + let employee = new Employee("Bob");
 142             + animal = rhino;
 143             + animal = employee; // Error: 'Animal' and 'Employee' are not compatible
 144         ittf
 145             +
 146                 class Animal
 147                     p name
 148                         :private
 149                         :string
 150                     ctor
 151                         param theName
 152                             :string
 153                         set this.name = theName
 154                 class Rhino
 155                     super Animal
 156                     ctor
 157                         _ super("Rhino")
 158                 class Employee
 159                     p name
 160                         :private
 161                         :string
 162                     ctor
 163                         param theName
 164                             :string
 165                         set this.name = theName
 166                 let animal = new Animal("Goat")
 167                 let rhino = new Rhino()
 168                 let employee = new Employee("Bob")
 169                 set animal = rhino
 170                 set animal = employee
 171     item
 172         title Class accessors
 173         expected
 174             + class Person {
 175                 + protected name: string;
 176                 + constructor(name: string) { this.name = name; }
 177             + }
 178             + class Employee extends Person {
 179                 + private department: string;
 180                 + constructor(name: string, department: string) {
 181                     + super(name);
 182                     + this.department = department;
 183                 + }
 184                 + public getElevatorPitch() {
 185                     + return `Hello, my name is ${this.name} and I work in ${this.department}.`;
 186                 + }
 187             + }
 188             + let howard = new Employee("Howard", "Sales");
 189             + console.log(howard.getElevatorPitch());
 190             + console.log(howard.name); // error
 191         ittf
 192             +
 193                 class Person
 194                     p name
 195                         :protected
 196                         :string
 197                     ctor
 198                         param name
 199                             :string
 200                         set this.name = name
 201                 class Employee
 202                     super Person
 203                     p department
 204                         :private
 205                         :string
 206                     ctor
 207                         param name
 208                             :string
 209                         param department
 210                             :string
 211                         _ super(name)
 212                         set this.department = department
 213                     m getElevatorPitch
 214                         :public
 215                         return
 216                             template
 217                                 + Hello, my name is${}
 218                                 @ this.name
 219                                 + ${}and I work in${}
 220                                 @ this.department
 221                                 + .
 222     item
 223         title Readonly modifier
 224         expected
 225             + class Octopus {
 226                 + readonly name: string;
 227                 + readonly numberOfLegs: number = 8;
 228                 + constructor(theName: string) {
 229                     + this.name = theName;
 230                 + }
 231             + }
 232             + let dad = new Octopus("Man with the 8 strong legs");
 233             + dad.name = "Man with the 3-piece suit"; // error! name is readonly.
 234         ittf
 235             +
 236                 class Octopus
 237                     p name
 238                         :readonly
 239                         :string
 240                     p numberOfLegs
 241                         :readonly
 242                         :number
 243                         := 8
 244                     ctor
 245                         param theName
 246                             :string
 247                         set this.name = theName
 248                 let dad = new Octopus("Man with the 8 strong legs")
 249                 set dad.name = "Man with the 3-piece suit"
 250     item
 251         title Parameter properties
 252         expected
 253             + class Octopus {
 254                 + readonly numberOfLegs: number = 8;
 255                 + constructor(readonly name: string) {
 256                 + }
 257             + }
 258         ittf
 259             +
 260                 class Octopus
 261                     p numberOfLegs
 262                         :readonly
 263                         :number
 264                         := 8
 265                     ctor
 266                         param
 267                             :readonly
 268                             :string
 269     item
 270         title Static members
 271         expected
 272             + class Grid {
 273                 + static origin = { x: 0, y: 0 };
 274                 + calculateDistanceFromOrigin(point: { x: number; y: number; }) {
 275                     + let xDist = (point.x - Grid.origin.x);
 276                     + let yDist = (point.y - Grid.origin.y);
 277                     + return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale;
 278                 + }
 279                 + constructor(public scale: number) { }
 280             + }
 281         ittf
 282             class Grid
 283                 p origin
 284                     static
 285                     {
 286                         @ x 0
 287                         @ y 0
 288                 m calculateDistanceFromOrigin
 289                     param point
 290                         :{
 291                             :p x
 292                                 :number
 293                             :p y
 294                                 :number
 295                     let xDist = (point.x - Grid.origin.x)
 296                     let yDist = (point.y - Grid.origin.y)
 297                     return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale
 298                 ctor
 299                     param scale
 300                         :public
 301                         :number
 302     item
 303         title Abstract classes 1
 304         expected
 305             + abstract class Animal {
 306                 + abstract makeSound(): void;
 307                 + move(): void {
 308                     + console.log("roaming the earth...");
 309                 + }
 310             + }
 311         ittf
 312             +
 313                 class Animal
 314                     :abstract
 315                     :m makeSound
 316                         :abstract
 317                         :return
 318                             :void
 319                     m move
 320                         :return
 321                             :void
 322                         _ console.log("roaming the earth...")
 323     item
 324         title Abstract classes 2
 325         expected
 326             + abstract class Department {
 327                 + constructor(public name: string) {
 328                 + }
 329                 + printName(): void {
 330                     + console.log("Department name: " + this.name);
 331                 + }
 332                 + abstract printMeeting(): void; // must be implemented in derived classes
 333             + }
 334             + class AccountingDepartment extends Department {
 335                 + constructor() {
 336                     + super("Accounting and Auditing"); // constructors in derived classes must call super()
 337                 + }
 338                 + printMeeting(): void {
 339                     + console.log("The Accounting Department meets each Monday at 10am.");
 340                 + }
 341                 + generateReports(): void {
 342                     + console.log("Generating accounting reports...");
 343                 + }
 344             + }
 345             + let department: Department; // ok to create a reference to an abstract type
 346             + department = new Department(); // error: cannot create an instance of an abstract class
 347             + department = new AccountingDepartment(); // ok to create and assign a non-abstract subclass
 348             + department.printName();
 349             + department.printMeeting();
 350             + department.generateReports(); // error: method doesn't exist on declared abstract type
 351         ittf
 352             +
 353                 class Department
 354                     :abstract
 355                     ctor
 356                         param name
 357                             :public
 358                             :string
 359                     m printName
 360                         :return
 361                             :void
 362                         _ console.log("Department name: " + this.name)
 363                     :m printMeeting
 364                         :abstract
 365                         :return
 366                             :void
 367                 class AccountingDepartment
 368                     super Department
 369                     ctor
 370                         _ super("Accounting and Auditing")
 371                     m printMeeting
 372                         :return
 373                             :void
 374                         _ console.log("The Accounting Department meets each Monday at 10am.")
 375                     m generateReports
 376                         :return
 377                             :void
 378                         _ console.log("Generating accounting reports...")
 379                 let department
 380                     :ref Department
 381                 set department = new Department()
 382                 set department = new AccountingDepartment()
 383                 _ department.printName
 384                 _ department.printMeeting
 385                 _ department.generateReports
 386                 # error: method doesn't exist on declared abstract type
 387     item
 388         title Static members
 389         expected
 390             + class Greeter {
 391                 + static standardGreeting = "Hello, there";
 392                 + greeting: string;
 393                 + greet() {
 394                     + if (this.greeting) {
 395                         + return "Hello, " + this.greeting;
 396                     + } else {
 397                         + return Greeter.standardGreeting;
 398                     + }
 399                 + }
 400             + }
 401         ittf
 402             +
 403                 class Greeter
 404                     p standardGreeting
 405                         static
 406                         := "Hello, there"
 407                     p greeting
 408                         :string
 409                     m greet
 410                         if this.greeting
 411                             return "Hello, " + this.greeting
 412                         else
 413                             return Greeter.standardGreeting
 414     item
 415         title Class extended by interface
 416         expected
 417             + class Point {
 418                 + x: number;
 419                 + y: number;
 420             + }
 421             + interface Point3d extends Point {
 422                 + z: number;
 423             + }
 424             + let point3d: Point3d = { x: 1, y: 2, z: 3 };
 425         ittf
 426             +
 427                 class Point
 428                     p x
 429                         :number
 430                     p y
 431                         :number
 432                 :interface Point3d
 433                     :extends Point
 434                     :p z
 435                         :number
 436                 let point3d
 437                     :ref Point3d
 438                     {
 439                         @ x 1
 440                         @ y 2
 441                         @ z 3