Skip to content

Latest commit

 

History

History
27 lines (20 loc) · 546 Bytes

File metadata and controls

27 lines (20 loc) · 546 Bytes

Classes

Classes are a way to implement object-oriented programming in JavaScript. They are automatically a type in TypeScript.

class User {
  constructor(public name: string, public age: number) {}
}

const user = new User("Alice", 25);
// ^ user: User

You may use instanceof to check if an object is an instance of a class:

function printDate(date: Date | string) {
  if (date instanceof Date) {
    console.log(date.toISOString());
    // ^ date: Date
    return;
  }

  console.log(new Date(date).toISOString());
}