-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
>; |