From 692d68d7a413f14224368cd4fdbddae78ecbf52f Mon Sep 17 00:00:00 2001 From: Rodrigo Queiroz Date: Sat, 23 Sep 2023 12:42:51 -0300 Subject: [PATCH] Add new solution to 29.5 without using Pick --- ...ick.solution.ts => 29.5-pick.solution1.ts} | 0 src/06-challenges/29.5-pick.solution2.ts | 44 +++++++++++++++++++ 2 files changed, 44 insertions(+) rename src/06-challenges/{29.5-pick.solution.ts => 29.5-pick.solution1.ts} (100%) create mode 100644 src/06-challenges/29.5-pick.solution2.ts 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", + ] + ); +});