Skip to content

Instantly share code, notes, and snippets.

@nilscox
Created September 9, 2025 21:29
Show Gist options
  • Select an option

  • Save nilscox/dfb23a75fb0c88fbb3d91c5f92f9ab2c to your computer and use it in GitHub Desktop.

Select an option

Save nilscox/dfb23a75fb0c88fbb3d91c5f92f9ab2c to your computer and use it in GitHub Desktop.
TypeScript exercices
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