property typescript code example

Example 1: typescript class interface

interface IPerson {
  name: string
  age: number
  hobby?: string[]
}

class Person implements IPerson {
  name: string
  age: number
  hobby?: string[]

  constructor(name: string, age: number, hobby: string[]) {
    this.name = name
    this.age = age
    this.hobby = hobby
  }
}

const output = new Person('john doe', 23, ['swimming', 'traveling', 'badminton'])
console.log(output)

Example 2: classes in typescript

class Info {
  private name: string ;
  constructor(n:string){
    this.name = n ;
  };
  describe(){
    console.log(`Your name is  ${this.name}`);
  }
}

const a = new Info('joyous');
a.describe();