Computer scienceProgramming languagesTypeScriptData TypesBasic Types

Never and unknown

Theory

Safe property access

Report a typo

Write a TypeScript function called safeAccess that takes an object of type unknown and a property name as a string. The function must safely access the property of the object and return its value if it exists. If the property doesn't exist or the input object is not an object, the function must return undefined. Fill in the blanks to complete the program.

Fill in the gaps with the relevant elements
function safeAccess(obj: , propName: ):  {
    if (typeof obj ===  && obj !== ) {
        return (obj as {[key: string]: unknown})[];
    }

    return ;
}
 
propNamenullundefined'object'unknownstringunknown
___

Create a free account to access the full topic