Skip to content

2nd block never gets executed in exercise CP_03_02  #1

@rkkarn32

Description

@rkkarn32

Below code section will never get executed. object x is of type string (Which is literal), and we are checking for instance String (Which is interface).

if(x instanceof String) {
        x.substr(0)
}

See below sample code

function totalLength(x: string, y: string): number
function totalLength(x: any[], y: any[]): number
function totalLength(x: (string | any[]), y: (string | any[])): number {
    var total: number = x.length + y.length;    
    if(x instanceof Array) {
        x.push('TypeScript')
    }   
    if(x instanceof String) {
        x = x.substr(1,3);
    }
    console.log(x)
    return total;
}
totalLength("first","second");

Expected output: eco
Actual output: first

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions