Bug in function overloads example in spec

Topics: Language Specification
Oct 7, 2012 at 12:52 PM

In the code example for Function Overloads, it has this code:


function
attr(name: string): string; function attr(name: string, value: string): Accessor; function attr(map: any): Accessor; function attr(nameOrMap: any, value: string): any { if (nameOrMap && typeof nameOrMap === "object") { // handle map case } else { // handle string case } }

But this code actually generates an error "Overload signature not compatible with function definition" for two of the overloads because the implementation does not mark the second parameter as optional. Seems like the code should be:

function attr(name: string): string;
function attr(name: string, value: string): Accessor;
function attr(map: any): Accessor;
function attr(nameOrMap: any, value?: string): any {
    if (nameOrMap && typeof nameOrMap === "object") {
        // handle map case
    }
    else {
        // handle string case
    }
}

Developer
Oct 7, 2012 at 1:18 PM

Good catch. Thanks!