Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

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();
Comment

Class Example In TypeScript


 class Person{


  private name: string;


  public constructor(name: string)
  {
this.name = name

  }
  
  public getName():string{
  return this.name;
  }
 }
 
var p =  new Person("Jane Doe");
console.log(p.getName());
/*this example is more proper than the previous, though the previous example is syntax-wise correct*/
Comment

typescript class type t

class GenericNumber<T> {
    zeroValue: T;
    add: (x: T, y: T) => T;
}

let myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function(x, y) { return x + y; };
Comment

typescript class

interface User {
  name: string;
  id: number;
}
 
class UserAccount {
  name: string;
  id: number;
 
  constructor(name: string, id: number) {
    this.name = name;
    this.id = id;
  }
}
 
const user: User = new UserAccount("Murphy", 1);
Try
Comment

TypeScript Class Example

class Animal {  public name: string;
  public constructor(theName: string) {    this.name = theName;  }
  public move(distanceInMeters: number) {    console.log(`${this.name} moved ${distanceInMeters}m.`);  }}Try
Comment

typescript class

export class Ingredient{
  //This is a shortcut to what is written below 
  constructor(public name:String, public amount:String){}
}

// On top is the same as below 
//
// export class Ingredient{
//     public name: String;
//     public amount: Number;
    
//     constructor(name:String , amount:Number) {
//         this.name = name;
//         this.amount = amount;
//     }
// }
Comment

Class In TypeScript


 class Person{


  public name: string;


  public constructor(name: string)
  {
this.name = name

  }
 }
 
var p =  new Person("Jane Doe");
console.log(p.name);

/*for typescript, you need to not only specify the type in the normall places(e.g. constructor instead of name is name:string), but you also need to specify what the methods are above the constructor*/
 /*not necessarily the recommended format for getting name, but at least this is gramatically correct*/
Comment

typescript class

export class Ingredient{
  //This is a shortcut to what is written below 
  constructor(public name:String, public amount:String){}
}

// On top is the same as below 
//
// export class Ingredient{
//     public name: String;
//     public amount: Number;
    
//     constructor(name:String , amount:Number) {
//         this.name = name;
//         this.amount = amount;
//     }
// }

Comment

classes in ts

class Greeter {  greeting: string;
  constructor(message: string) {    this.greeting = message;  }
  greet() {    return "Hello, " + this.greeting;  }}
let greeter = new Greeter("world");Try
Comment

Typescript class

// First Compile TS Code to JS Code using this command line
// --- tsc -t es5 <NameOfTheFile>.ts
// Then open the HTML browser using live server
// For Html part you clone my Github Repo :)
// https://github.com/karouhifar/TypeScript

const buttom = document.getElementById("button");
const num1Dom = document.getElementById("num1")! as HTMLInputElement;
const num2Dom = document.getElementById("num2")! as HTMLInputElement;
const num3Dom = document.getElementById("num3")! as HTMLInputElement;
const tBody = document.getElementById("dataJSON")! as HTMLElement;
const result = document.getElementById("result")! as HTMLElement;

declare type Combinable = Array<string | number>;

interface PersonName {
    name ?: string
}
interface NumData extends PersonName {
    doubleAmount: (double: number, nameData: string) => string;
}
class Data implements NumData {
    constructor (private num1:number,private num2:number) {}
    get getNumber() : number {
        return this.num1 + this.num2;
    }

    doubleAmount(double, nameData) : string {
       let name : PersonName = {name : nameData} as PersonName;
       console.log(name);
       console.log(this.getNumber);
        return this.getNumber * double + " " + name.name;
    }
}

async function  getJSONData () {
   const dataJSON : Response  =  await fetch("./data.json").then();
    return dataJSON;
}



buttom.addEventListener("click", ()=> {
    let data = new Data(+num1Dom.value,+num2Dom.value);
    let result1 = data.doubleAmount(+num3Dom.value, "Jack");
    getJSONData().then((json)=> json.json() ).then((data: Array<{name : string; age: Number, hobbies: Combinable}>)=>{
        let stringData : string = "";
       for (const dataArray of data ) {
        stringData += "<tr><td>" + dataArray.name + "</td><td>" + dataArray.age +"</td><td>" + dataArray.hobbies +"</td></tr>";
       }

       tBody.innerHTML = stringData; 
    });

    //  or  data.setNumber(numbers);
    result.innerHTML = result1 as any;
    // or  result.innerHTML = <any> data.getNumber;
});
Comment

Class Example In TypeScript

class Person{


  private name: string;


  public constructor(name: string)
  {
this.name = name

  }
  
  public getName():string{
  return this.name;
  }
 }
 
var p =  new Person("Jane Doe");
console.log(p.getName());
/*this example is more proper than the previous, though the previous example is syntax-wise correct*/
Comment

Class In TypeScript

class Person{


  public name: string;


  public constructor(name: string)
  {
this.name = name

  }
 }
 
var p =  new Person("Jane Doe");
console.log(p.name);

/*for typescript, you need to not only specify the type in the normall places(e.g. constructor instead of name is name:string), but you also need to specify what the methods are above the constructor*/
 /*not necessarily the recommended format for getting name, but at least this is gramatically correct*/
Comment

PREVIOUS NEXT
Code Example
Typescript :: getserversideprops vs getstaticprops 
Typescript :: angular currency pipe pt-br as variable 
Typescript :: create user properties firebase 
Typescript :: deep partial typescript 
Typescript :: ionic cannot be loaded because running scripts is disabled on this system. vscode 
Typescript :: typescript compile on save 
Typescript :: node js process on unhandled promise rejection 
Typescript :: --skip tests generate components - Angular 12 - skip-tests vs spec-false 
Typescript :: go through each element of a dictionary typescript 
Typescript :: td elements in same line 
Typescript :: Already included file name react tsconfig 
Typescript :: material form 
Typescript :: github sync local with remote 
Typescript :: define typescript types 
Typescript :: contract method calling with ether.js 
Typescript :: typescript parameter function type 
Typescript :: type script array 
Typescript :: sweetalert2 
Typescript :: execute script when c# code gets executed 
Typescript :: nest js null exclude 
Typescript :: update behaviorsubject value without emitting 
Typescript :: typescript playground 
Typescript :: How can I call a method every x seconds? 
Typescript :: how to get the table contents from a file in python 
Typescript :: read/write linked lists to file 
Typescript :: Why do we use fragments in react? 
Typescript :: this typescript 
Typescript :: ts enum 
Typescript :: how to take inputs in one line in c 
Typescript :: Comparison method violates its general contract! 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =