Dev Helpers
← Back

Pluck a keyed value generically

Pluck a keyed value generically using TypeScript.

Open this example in the interactive library

Source code

const words = ["build", "ship", "test"];
console.log(
  (<T, K extends keyof T>(obj: T, key: K): T[K] => obj[key])(
    { name: "Ada", rank: 2 },
    "rank",
  ),
);

Sample output

2