Repository navigation
Expand file tree
/
Copy pathuseLocalStorage.type-compiler.test.ts
More file actions
52 lines (41 loc) · 1.75 KB
/
Copy pathuseLocalStorage.type-compiler.test.ts
File metadata and controls
52 lines (41 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { describe, test, expectTypeOf } from 'vitest';
import { useLocalStorage } from './useLocalStorage';
describe('TypeScript Compiler Validation & Schema Constraints Stability', () => {
test('useLocalStorage should match the expected generic hook contract', () => {
type UseLocalStorageSignature = <T>(
key: string,
initialValue: T
) => readonly [T, (value: T) => void];
expectTypeOf(useLocalStorage).toMatchTypeOf<UseLocalStorageSignature>();
});
test('useLocalStorage should support primitive value contracts', () => {
type PrimitiveReturn = ReturnType<typeof useLocalStorage<string>>;
expectTypeOf<PrimitiveReturn>().toEqualTypeOf<readonly [string, (value: string) => void]>();
});
test('useLocalStorage should preserve object field property configurations', () => {
type User = {
name: string;
score: number;
isPro: boolean;
};
type UserReturn = ReturnType<typeof useLocalStorage<User>>;
expectTypeOf<UserReturn>().toEqualTypeOf<readonly [User, (value: User) => void]>();
});
test('useLocalStorage should accept custom types with optional values', () => {
type Preferences = {
theme: string;
fontSize?: number;
compactMode?: boolean;
};
type PreferencesReturn = ReturnType<typeof useLocalStorage<Preferences>>;
expectTypeOf<PreferencesReturn>().toEqualTypeOf<
readonly [Preferences, (value: Preferences) => void]
>();
});
test('useLocalStorage setter contract should reject incompatible function shapes', () => {
type CountReturn = ReturnType<typeof useLocalStorage<number>>;
type CountSetter = CountReturn[1];
type InvalidSetter = (value: string) => void;
expectTypeOf<InvalidSetter>().not.toMatchTypeOf<CountSetter>();
});
});