viernes, 15 de marzo de 2019

Repasando TypeScript

Repasando de forma rápida la sintaxis básica de TypeScript.
//---------- tipo de datos

var myString: string ="hello world";
myString = 22 +"";
document.write(myString);

var myNumber: number = 66;
document.write(myNumber.toString());

var myBoolean: boolean = true;
document.write(myBoolean.toString());

var myVar: any = "hello";
myVar = false;
document.write(myVar);


//---------- Arreglo
var anyArray: any[] = [1,2,"cadena1","cadena2",[], {},""];
var StringArray: string[] = ["item1","item2",""];
var booleanArray: boolean[]= [true,false,true];

//--------tuplas
var strNumTuple: [string, number];
strNumTuple =["hola",77];


//--------void - undefined - null
let myVoid: void = undefined;
let myNull: null = null;
let myUndefined: undefined = undefined;

document.write(typeof(myVoid));


//--------funciones
function getSum(num1: number, num2: number): number{
    return num1+num2;
}

let mysum = function(num1: number | string, num2: number | string):number{
    if(typeof(num1) === 'string'){
        num1 = parseInt(num1);
    }
    if(typeof(num2) === 'string'){
        num2 = parseInt(num2);
    }
    return num1 + num2;
}

function getName(firstName: string,
    lastName?:string): string{       //el atributo con ? es un parametro opcional
    if(lastName == undefined){
        return firstName;
    }
    //return firstName + " " + lastName;      ES5
    return `${firstName} ${lastName}`;    //  ES6
}
document.write(getName("Luis"));



//-------Interfaces
interface ITodo{
    title: string;
    text: string;
}
function showTodo(todo: ITodo){
//function showTodo(todo: {title: string; text: string}){
    console.log(`${todo.title} - ${todo.text}`);
}

showTodo({
    title: "Eat Dinner",
    text: "lorem"
})

//----- CLASES

class User {
    private name: string; // no es accesile fuera de la clase
    public email: string; //accesible fuera de la clase
    protected age: number; //no es accesible
   
    //constructor, sera llamado apenas sea istanciado
    constructor(name:string, email:string, age: number){
        this.name = name;
        this.email = email;
        this.age = age;
    }

    //metodo
    register(){
        console.log(`${this.name} is registeder`);
    }
    showMeAge(){
        return this.age;
    }
    public AgeInYears(){
        return this.age + ' years';
    }
    payInvoice(){
        console.log(`${this.name} pago su factura`);
    }

}

var jhon = new User('qwerty', "alfred@asdfg", 28);

document.write(jhon.name); //this = jhon
console.log(jhon.register());

//------ Herencia
class Menber extends User {
    id: number;

    constructor(id: number, name: string, email: string, age: number){
        super(name, email, age);
        this.id= id;
    }
    //metodo heredado de la clase padre
    payInvoice(){
        super.payInvoice();
    }
}
var gordon  = new Menber(1, 'jose', 'jose@MediaQueryList', 11);

gordon.payInvoice();

No hay comentarios.:

Publicar un comentario

Repasando TypeScript

Repasando de forma rápida la sintaxis básica de TypeScript. //----------  tipo de datos var myString: string ="hello world"; m...