From e3cd869765a261679aa309ee013ea4721445c23f Mon Sep 17 00:00:00 2001 From: zyhou Date: Fri, 15 Dec 2023 09:43:51 +0100 Subject: [PATCH] addd day 15 --- 15/index.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 15/index.ts diff --git a/15/index.ts b/15/index.ts new file mode 100644 index 0000000..d0cf06c --- /dev/null +++ b/15/index.ts @@ -0,0 +1,28 @@ +import { Expect, Equal } from "type-testing"; + +// Same as my day 13 solution +// It used Distributive Conditional Types +// https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types +type BoxToys< + Word extends string, + Length, + Accumulator extends string[] = [], +> = Length extends Accumulator["length"] + ? Accumulator + : BoxToys; + +// ------------------- Test section --------------------- + +type test_doll_actual = BoxToys<"doll", 1>; +// ^? +type test_doll_expected = ["doll"]; +type test_doll = Expect>; + +type test_nutcracker_actual = BoxToys<"nutcracker", 3 | 4>; +// ^? +type test_nutcracker_expected = + | ["nutcracker", "nutcracker", "nutcracker"] + | ["nutcracker", "nutcracker", "nutcracker", "nutcracker"]; +type test_nutcracker = Expect< + Equal +>;