Learn Typescript > Type Narrowing challenge issue

Teacher asks:

 /**
     * Challenge: write the code to check if the parameter is a string
     * or a number, and use the menu.find() method accordingly
     */

I cant use “identifier string | number” inside the if statement because it doesnt work.
I also dont understand what he wants me to do exactly. Am I just dumb? Teacher isnt explaining what he wants us to do…

function getPizzaDetail(identifier: string | number) {
    /**
     * Challenge: write the code to check if the parameter is a string
     * or a number, and use the menu.find() method accordingly
     */
    
    if (identifier string | number) {
        
    }
}

The following is what he wants us to write:

function getPizzaDetail(identifier: string | number) {
    if (typeof identifier === "string") {
        return menu.find(pizza => pizza.name.toLowerCase() === identifier.toLowerCase())
    } else {
        return menu.find(pizza => pizza.id === identifier)
        
    }
}

I didnt know to use “type of”, i didnt know if that i shouldve written something about “pizza”, “tolowercase” to use === with it, also he told us to only write the if statement, not else…

Tagging @bobziroll or @Tom_Chant for more insight.

1 Like

Thank you very much!