Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

ts class

class Greeter {  
  greeting: string;
  
  constructor(message: string) {
    this.greeting = message;  
  }
  
  greet() {    
    return "Hello, " + this.greeting;
  }}

let greeter = new Greeter("world");Try
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

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

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

PREVIOUS NEXT
Code Example
Typescript :: ternary operator typescript 
Typescript :: To list all tcp ports. 
Typescript :: angular modal dismisss 
Typescript :: ts console.log 
Typescript :: angular append array to another 
Typescript :: extends vs implements java 
Typescript :: change url param angular 
Typescript :: prettier eslint typescript 
Typescript :: try catch powershell error message 
Typescript :: typescript debounce 
Typescript :: select column values from array typescript 
Typescript :: extend type typescript 
Typescript :: typescript exclamation mark 
Typescript :: what namespace are lists 
Typescript :: prisma user model 
Typescript :: typescript array of object with types 
Typescript :: nodejs aws s3 upload 
Typescript :: laravel validation check if email exists forget password 
Typescript :: getserversideprops vs getstaticprops 
Typescript :: npm run serve https 
Typescript :: typescript append row in html table 
Typescript :: path react native 
Typescript :: interface array typescript 
Typescript :: typescript variables 
Typescript :: typeorm find with limit 
Typescript :: Angular import from local library 
Typescript :: typescript make object optional 
Typescript :: from how many ways we can define props with typescript react 
Typescript :: remove showing results woocommerce shortcode 
Typescript :: angular find and remove from string 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =