Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

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

Open
xmlking opened this issue Jul 22, 2014 · 0 comments

Comments

@xmlking
Copy link

xmlking commented Jul 22, 2014

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 added a commit to xmlking/assert that referenced this issue Jul 22, 2014
Type Assertions for positional plus optional/named Arguments
angular#7
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant