TECNOLOBO

No recuerdas tu codigo?
Se te olvido como se hace?

Aqui podras guardar lo que necesiten
Y cuando sea necesesario

Creado por julian gomez
iiiiii

validar Propiedades en clase



Descripcion

Validar propiedades en una clase antes al instanci

javascript


type Size = ''| 'S'|'M'|'XL';

class Product {
    constructor(
        public name: string = '',
        public price: number = 0,
        public size: Size = '',
    ){}

    isProductReady(): boolean {
        //el this en una clase trae cada uno de los parametros de la clase por elemeplo el for realizara name,prince,size y para acceder a su calor seria this.name
        for( const key in this ) {
            switch( typeof this[key] ) {
                case 'string':
                    if ( (<string><unknown>this[key]).length <= 0 ) throw Error(`${ key } is empty`);
                break;
                case 'number':
                    if ( (<number><unknown>this[key]) <= 0 ) throw Error(`${ key } is zero`);
                break;
                default:
                    throw Error(`${ typeof this[key] } is not valid`);
            }
        }

        return true;
    }

    
    toString() {
        
        if ( !this.isProductReady ) return;
        

        return `${ this.name } (${ this.price }), ${ this.size }`
    }

}

(()=> {

    const bluePants = new Product('Blue Pants', 10,'S');
    console.log(bluePants.toString());

})();