Skip to content

Commit

Permalink
Test: Types - Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
black7375 committed Jan 11, 2024
1 parent 6d793cb commit 6d41a85
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/transform-to-vanilla/src/types/style-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ type ResolvedProperties = Properties<number | NonNullableString>;
type CSSKeyframeFromTo =
| "from"
| "to"
| `${IntRange<1, 11>}0%`
| `${IntRange<1, 10>}0%`
| `${number & NonNullable<unknown>}%`;

// == Tests ====================================================================
Expand Down
36 changes: 32 additions & 4 deletions packages/transform-to-vanilla/src/types/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type IntRange<F extends number, T extends number> = Exclude<
Enumerate<T>,
Enumerate<F>
>;
// https://stackoverflow.com/questions/39494689/is-it-possible-to-restrict-number-to-a-certain-range
export type IntRange<F extends number, T extends number> =
| Exclude<Enumerate<T>, Enumerate<F>>
| T;

export type Enumerate<
N extends number,
Expand All @@ -11,3 +11,31 @@ export type Enumerate<
: Enumerate<N, [...Acc, Acc["length"]]>;

export type ExcludeArray<T> = T extends unknown[] ? never : T;

if (import.meta.vitest) {
const { describe, it, expectTypeOf } = import.meta.vitest;

describe.concurrent("Type utils", () => {
it("IntRage<F, T>", () => {
expectTypeOf<IntRange<0, 0>>().toEqualTypeOf<0>();
expectTypeOf<IntRange<0, 1>>().toEqualTypeOf<0 | 1>();
expectTypeOf<IntRange<0, 3>>().toEqualTypeOf<0 | 1 | 2 | 3>();
expectTypeOf<IntRange<5, 10>>().toEqualTypeOf<5 | 6 | 7 | 8 | 9 | 10>();
});

it("Enumerate<T, Acc>", () => {
expectTypeOf<Enumerate<0>>().toEqualTypeOf<never>();
expectTypeOf<Enumerate<1>>().toEqualTypeOf<0>();
expectTypeOf<Enumerate<3>>().toEqualTypeOf<0 | 1 | 2>();
expectTypeOf<Enumerate<5>>().toEqualTypeOf<0 | 1 | 2 | 3 | 4>();
});

it("ExcludeArray<T>", () => {
expectTypeOf<ExcludeArray<string | string[]>>().toEqualTypeOf<string>();
expectTypeOf<ExcludeArray<string | number[]>>().toEqualTypeOf<string>();
expectTypeOf<ExcludeArray<boolean | number | number[]>>().toEqualTypeOf<
boolean | number
>();
});
});
}

0 comments on commit 6d41a85

Please sign in to comment.