diff --git a/src/06-challenges/29.5-pick.solution.ts b/src/06-challenges/29.5-pick.solution1.ts similarity index 100% rename from src/06-challenges/29.5-pick.solution.ts rename to src/06-challenges/29.5-pick.solution1.ts diff --git a/src/06-challenges/29.5-pick.solution2.ts b/src/06-challenges/29.5-pick.solution2.ts new file mode 100644 index 0000000..2bf164b --- /dev/null +++ b/src/06-challenges/29.5-pick.solution2.ts @@ -0,0 +1,44 @@ +import { expect, it } from "vitest"; +import { Equal, Expect } from "../helpers/type-utils"; + +const pick = ( + obj: TObj, + picked: Array +) => { + return picked.reduce((acc, key) => { + acc[key] = obj[key]; + return acc; + }, {} as {[PickedKey in TPicked]: TObj[PickedKey]}); +}; + +it("Should pick the keys from the object", () => { + const result = pick( + { + a: 1, + b: 2, + c: 3, + d: true + }, + ["a", "b", "d"] + ); + + expect(result).toEqual({ a: 1, b: 2 }); + + type test = Expect>; +}); + +it("Should not allow you to pass keys which do not exist in the object", () => { + pick( + { + a: 1, + b: 2, + c: 3, + }, + [ + "a", + "b", + // @ts-expect-error + "d", + ] + ); +});