Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
This repository was 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

@xmlking

Description

@xmlking

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'})); 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions