@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function getKeyPath

    • get value of nested obj at a given key-path.

      Type Parameters

      Parameters

      • obj: T
      • kpath: KP

      Returns KeyPathValue<T, KP>

      import { assertEquals } from "jsr:@std/assert"

      const data = { kill: { your: { self: [0, 1, { 0: 0, 1: { noice: "YAHAHA", 0: "you found me!" } }] } } } as const

      // weakly typed variant
      const
      keypath_to_noice_parent = ["kill", "your", "self", 2, 1], // note: adding `as const` will not make it strongly typed because of a limitation/bug in `KeyPathsOf`
      noice_parent = getKeyPath(data, keypath_to_noice_parent) // type: `unknown`

      assertEquals(noice_parent, { noice: "YAHAHA", 0: "you found me!" })

      // strongly typed variant
      const
      strong_keypath_to_noice_parent: ["kill", "your", "self", 2, 1] = ["kill", "your", "self", 2, 1],
      strong_noice_parent: { noice: "YAHAHA", 0: "you found me!" } = getKeyPath(data, strong_keypath_to_noice_parent)

      assertEquals(strong_noice_parent, { noice: "YAHAHA", 0: "you found me!" })