assert icon indicating copy to clipboard operation
assert copied to clipboard

Type Assertions for Named/Optional Arguments should only assert when provided.

Open xmlking opened this issue 11 years ago • 0 comments

Trying to apply Type Assertions for a function that takes optional arguments. when the optional arguments are not provided, it throws errors. will it be nice if assert.argumentTypes check and only apply type checking if optional argument is not undefined? in the example below, value argument is optional and type is number. My suggestion is, validate type only if an optional argument is supplied.

export class EnumItem {
    // constructor(name: string, {value} = {}, value: number) { //TODO want to use this 
    constructor(name: string, {value = Infinity}, value: number) { //workaround
        this.name  = name;
        // this.value = (value) ? value: Symbol(name);  //want to use this 
        this.value = (value === Infinity) ? Symbol(name) : value; //workaround


        delete arguments[1].value;
        Object.assign(this, arguments[1]);

        Object.freeze(this);
    }

    toString() {
        return this.name;
    }

    valueOf() {
        return this.value;
    }
}
console.log('EnumItem1', new EnumItem('xname',{value:2}));
console.log('EnumItem2', new EnumItem('xname',{ description:'ssss'})); 

xmlking avatar Jul 22 '14 05:07 xmlking