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

    Function setKeyPath

    • set the value of nested obj at a given key-path, and then returns the object back.

      Type Parameters

      Parameters

      Returns T

      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
      // note: adding `as const` will not make it strongly typed because of a limitation/bug in `KeyPathsOf`
      const keypath_to_yahaha = ["kill", "your", "self", 2, 1, "noice"]
      setKeyPath(data, keypath_to_yahaha, "YEEEE")

      assertEquals(getKeyPath(data, keypath_to_yahaha), "YEEEE")

      // strongly typed variant
      const strong_keypath_to_yahaha: ["kill", "your", "self", 2, 1, "noice"] = ["kill", "your", "self", 2, 1, "noice"]
      // @ts-ignore: since `data` is declared as `const`, we cannot technically assign the value `"YOSHIII"` to the original `"YAHAHA"`.
      setKeyPath(data, strong_keypath_to_yahaha, "YOSHIII")

      assertEquals(getKeyPath(data, strong_keypath_to_yahaha), "YOSHIII")