Function getProp

  • Gets a property value from an object using a dot-notation path

    Type Parameters

    • T extends NestedObject
    • P extends string

    Parameters

    • obj: T

      The object to get the property from

    • path: P

      The dot-notation path to the property (e.g., 'user.name')

    • Optional defaultValue: any

      (Optional) Value to return if property is not found

    Returns any

    The value of the property, defaultValue if provided and property not found, or undefined

    Throws

    Error if input validation fails

    Example

    const obj = { user: { name: 'Flavio Ever' } };
    getProp(obj, 'user.name'); // Returns 'Flavio Ever'
    getProp(obj, 'user.age', 30); // Returns 30 (default value)
    getProp(obj, 'user.nonExistent'); // Returns undefined