Skip to content

Commit

Permalink
addd day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
zyhou committed Dec 15, 2023
1 parent c105643 commit e3cd869
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 15/index.ts
Original file line number Diff line number Diff line change
@@ -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<Word, Length, [...Accumulator, Word]>;

// ------------------- Test section ---------------------

type test_doll_actual = BoxToys<"doll", 1>;
// ^?
type test_doll_expected = ["doll"];
type test_doll = Expect<Equal<test_doll_expected, test_doll_actual>>;

type test_nutcracker_actual = BoxToys<"nutcracker", 3 | 4>;
// ^?
type test_nutcracker_expected =
| ["nutcracker", "nutcracker", "nutcracker"]
| ["nutcracker", "nutcracker", "nutcracker", "nutcracker"];
type test_nutcracker = Expect<
Equal<test_nutcracker_expected, test_nutcracker_actual>
>;

0 comments on commit e3cd869

Please sign in to comment.