Created
September 9, 2025 21:29
-
-
Save nilscox/dfb23a75fb0c88fbb3d91c5f92f9ab2c to your computer and use it in GitHub Desktop.
TypeScript exercices
This file contains hidden or 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
| import { expectTypeOf, test } from 'vitest'; | |
| test('Join', () => { | |
| type Values = ['a'] | ['a', 'b'] | ['a', 'c'] | ['c', 'd', 'e']; | |
| type Result = Join<Values, '.'>; | |
| type Expected = 'a' | 'a.b' | 'a.c' | 'c.d.e'; | |
| expectTypeOf<Result>().toEqualTypeOf<Expected>(); | |
| }); | |
| test('Paths', () => { | |
| type Obj = { | |
| a: 'a'; | |
| b: { | |
| c: 'b.c'; | |
| }; | |
| d: { | |
| e: { f: 'd.e.f' }; | |
| }; | |
| }; | |
| type Result = Compute<Paths<Obj>>; | |
| type Expected = ['a'] | ['b', 'c'] | ['d', 'e', 'f']; | |
| expectTypeOf<Result>().toEqualTypeOf<Expected>(); | |
| }); | |
| test('InnerPaths', () => { | |
| type Obj = { | |
| a: 'a'; | |
| b: { | |
| c: 'b.c'; | |
| }; | |
| d: { | |
| e: { f: 'd.e.f' }; | |
| }; | |
| }; | |
| type Result = Compute<InnerPaths<Obj>>; | |
| type Expected = ['a'] | ['b'] | ['b', 'c'] | ['d'] | ['d', 'e'] | ['d', 'e', 'f']; | |
| expectTypeOf<Result>().toEqualTypeOf<Expected>(); | |
| }); | |
| test('Flatten', () => { | |
| type Obj = { | |
| a: 'a'; | |
| b: { | |
| a: 'b.a'; | |
| b: 'b.b'; | |
| }; | |
| c: { | |
| a: 'c.a'; | |
| b: { | |
| a: 'c.b.a'; | |
| b: 'c.b.b'; | |
| }; | |
| }; | |
| }; | |
| type Result = Flatten<Obj>; | |
| type Expected = { | |
| a: 'a'; | |
| 'b.a': 'b.a'; | |
| 'b.b': 'b.b'; | |
| 'c.a': 'c.a'; | |
| 'c.b.a': 'c.b.a'; | |
| 'c.b.b': 'c.b.b'; | |
| }; | |
| expectTypeOf<Result>().toEqualTypeOf<Expected>(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment