Last active
September 19, 2024 15:58
-
-
Save binjospookie/e81e7bc075ad9431f8e82c11f6c1c59d to your computer and use it in GitHub Desktop.
test
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
/* eslint-disable react-hooks/rules-of-hooks */ | |
import { createStore } from '@grlt/vendors/state'; | |
import { createHookWithStoreGetter } from '../index'; | |
const $myKv = createStore({ | |
a: 1, | |
b: '2', | |
c: { | |
d: true, | |
e: { | |
f: ['1', 2], | |
g: null, | |
}, | |
}, | |
h: undefined, | |
}); | |
const useMyKv = createHookWithStoreGetter($myKv); | |
{ | |
type Params = 'a' | 'b' | 'c' | 'c.d' | 'c.e' | 'c.e.f' | `c.e.f.${number}` | 'c.e.g' | 'h'; | |
expectTypeOf<Params>().toEqualTypeOf<Parameters<typeof useMyKv>[0][number]>(); | |
} | |
{ | |
type R = { a: number }; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['a'])); | |
} | |
{ | |
type R = { b: string }; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['b'])); | |
} | |
{ | |
type R = { c: { d: boolean; e: { f: (string | number)[]; g: null } } }; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['c'])); | |
} | |
{ | |
type R = { c: { d: boolean } }; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['c.d'])); | |
} | |
{ | |
type R = { c: { e: { f: (string | number)[]; g: null } } }; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['c.e'])); | |
} | |
{ | |
type R = { c: { e: { f: (string | number)[] } } }; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['c.e.f'])); | |
} | |
{ | |
type R = { c: { e: { g: null } } }; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['c.e.g'])); | |
} | |
{ | |
type R = { h: undefined }; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['h'])); | |
} | |
{ | |
type R = { a: number } & { b: string } & { h: undefined } & { c: { e: { g: null } } }; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['a', 'b', 'c.e.g', 'h'])); | |
} | |
{ | |
type R = string; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['a', 'b'], (x) => x.b + x.a)); | |
} | |
{ | |
type R = string[]; | |
expectTypeOf<R>().toEqualTypeOf(useMyKv(['a', 'b'], () => ['1'])); | |
} | |
{ | |
type R = number; | |
const variable = 2; | |
expectTypeOf<R>().toEqualTypeOf( | |
useMyKv(['a', 'b'], { | |
fn: (x) => x.a * variable, | |
keys: [variable], | |
}), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment