get
Gets a value from an object using a dot/bracket-notated path or explicit key array.
Two path forms are supported:
-
String path — dot notation (
'a.b.c') and bracket notation ('layers[1].name') are both accepted and mixed freely.- Dot segments are always string keys:
'a.1'→ key'1'(string). - Bracket segments are always number keys:
'a[1]'→ key1(number). - String literal paths give full compile-time type inference on the return type.
- Dynamic (non-literal) strings return
unknown.
- Dot segments are always string keys:
-
Key array (
PropertyKey[]) — explicit array ofstring | number | symbolkeys, no parsing performed. Enables symbol-keyed traversal and compile-time type inference:get(obj, ['a', 'b'] as const)infers the return type from the path.
Both forms support all objects (plain objects, arrays, class instances). Symbol keys are only reachable via the key-array form.
Available since v1.9.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
obj | null | undefined | The object to read from |
path | string | readonly PropertyKey[] | Dot/bracket-notation string literal or explicit PropertyKey[] |
defaultValue | D | Returned when the path is absent or resolves to undefined |
Returns
Section titled “Returns”D — The value at the path, or defaultValue
Examples
Section titled “Examples”Access a nested property
Section titled “Access a nested property”Uses a dot-notated path to retrieve a deeply nested value.
Return default for missing path
Section titled “Return default for missing path”Returns the default value when the path does not exist.
Get via key array (supports symbols)
Section titled “Get via key array (supports symbols)”Pass an explicit PropertyKey[] to bypass parsing. Supports string, number, and symbol keys.
