Examples

// Classes and Interfaces
interface Person {
  name: string;
  birthDate: Date;
}

class Patient implements Person {
  name: string = 'patientName';
  birthDate: Date;

  constructor(name: string, birthDate: Date) {
    this.name = name;
    this.birthDate = birthDate;
  }

  getName(): string {
    return this.name;
  }

  getBirthDate(): Date {
    return this.birthDate;
  }
}

// Creating an object
const name: string = "John Smith";
const birthDate: Date = new Date('01-01-1970');
const isNewPatient: boolean = true;

const patient: Patient = new Patient(name, birthDate);

// Variables and Data Types
let age: number = 30;
const isMale: boolean = true;
const favoriteFruits: string[] = ['apple', 'banana', 'orange'];

// Arrays and Objects
const patientData: { name: string, birthDate: Date, isNewPatient: boolean } = {
  name,
  birthDate,
  isNewPatient,
};

// Conditional statements
if (isNewPatient) {
  console.log("Is new patient");
} else {
  console.log("Is returning patient");
}

// Loops
for (let fruit of favoriteFruits) {
  console.log(fruit);
}

// Functions
function addNumbers(x: number, y: number): number {
  return x + y;
}

// Exception Handling
try {
  // Creating an object and Exception Handling
  const patient = new Patient(name, birthDate);
  patient.name = '';
} catch (e) {
  console.log('Error: ' + e.message);
}
Copied!