Skip to content

Commit 77e181b

Browse files
typing and test improvements
1 parent 038db3e commit 77e181b

File tree

8 files changed

+132
-48
lines changed

8 files changed

+132
-48
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-cache-store",
33
"type": "module",
4-
"version": "2.2.1",
4+
"version": "2.2.3",
55
"packageManager": "[email protected]",
66
"description": "Cache and re-use computed/reactive properties in vue",
77
"author": {
@@ -65,7 +65,7 @@
6565
"rollup-plugin-dts": "^6.1.1",
6666
"rollup-plugin-esbuild": "^6.1.1",
6767
"rollup-plugin-typescript2": "^0.36.0",
68-
"typescript": "^5.5.4",
68+
"typescript": "^5.9.2",
6969
"vitest": "3.2.4",
7070
"vue": "^3.5.17"
7171
},

pnpm-lock.yaml

Lines changed: 30 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/toWritableComputed.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ export type ToWritableComputed<T = any> = {
44
[K in keyof T]: WritableComputedRef<T[K]>;
55
};
66

7-
/**
8-
*
9-
* @param obj
10-
*/
117
export function toWritableComputed<T extends object>(obj: Ref<T> | ComputedRef<T> | WritableComputedRef<T>) {
128
const rawStore = toValue(obj)
139
const refs = {}

src/watchRecordStore.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { computed, type ComputedRef, watch } from 'vue'
22
import { makeRecordStore, type RecordStore } from './makeRecordStore'
33

44
export function watchRecordStore<
5-
ID extends NonNullable<any> = Parameters<Parameters<typeof watchRecordStore>[0]>[0],
6-
R extends object = NonNullable<ReturnType<Parameters<typeof watchRecordStore>[0]>>,
7-
T extends object = ReturnType<Parameters<typeof watchRecordStore>[1]>,
5+
ID extends NonNullable<any>,
6+
R extends object,
7+
T extends object,
88
>
99
(
1010
getRecord: (id: ID) => R | undefined,

tests/makeRecordStore.component.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { makeRecordStore } from '../src'
33
import { mount } from '@vue/test-utils'
44
import { computed, reactive, ref, toRef } from 'vue'
55

6-
describe('define cache store', async () => {
6+
describe('makeCacheStore() components', async () => {
77

88
it('ref()', async () => {
99
const x = ref('a')

tests/makeRecordStore.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
22
import { makeRecordStore } from '../src'
33
import { computed, type ComputedRef, nextTick, ref, watch } from 'vue'
44

5-
describe('define cache store', async () => {
5+
describe('makeCacheStore()', async () => {
66

77
it('only run once', async () => {
88
const count = ref(0)

tests/makeRecordStore.types.test.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@ import { describe, expectTypeOf, it } from 'vitest'
22
import type { Reactive, ToRefs } from 'vue'
33
import { type GenericRecordStore, makeRecordStore, type RecordStore } from '../src'
44

5-
describe('defineCacheStore() types', async () => {
5+
6+
type Item = {
7+
id: number,
8+
name: string,
9+
}
10+
11+
type ItemInfo = {
12+
id: number,
13+
name: string,
14+
context: RecordStore<number, ItemInfo>
15+
}
16+
17+
describe('makeCacheStore() types', async () => {
618

719
it('type GenericCacheStore', async () => {
820
function creatorFunction(id: number) {
@@ -16,13 +28,31 @@ describe('defineCacheStore() types', async () => {
1628
expectTypeOf(store).toEqualTypeOf<GenericRecordStore>()
1729
})
1830

19-
it('makeRecordStore() types: id number', () => {
31+
it('test inferred types', async () => {
2032

21-
type ItemInfo = {
22-
id: number,
23-
name: string,
24-
context: RecordStore<number, ItemInfo>
25-
}
33+
const store = makeRecordStore((id: number, context): Item => {
34+
35+
if (id !== 44) {
36+
const otherItem = context.get(44)
37+
expectTypeOf(otherItem).toEqualTypeOf<Item>()
38+
}
39+
40+
expectTypeOf<
41+
Parameters<typeof context.get>[0]
42+
>().toEqualTypeOf<number>()
43+
44+
return {
45+
id,
46+
name: 'susan',
47+
}
48+
})
49+
50+
const item = store.get(99)
51+
52+
expectTypeOf(item).toEqualTypeOf<Item>()
53+
})
54+
55+
it('makeRecordStore() types: id number', () => {
2656

2757
const cache = makeRecordStore<number, ItemInfo>((id, context) => {
2858
return {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { describe, expectTypeOf, it } from 'vitest'
2+
import { watchRecordStore } from '../src'
3+
import { type Person, type PersonInfo, usePeople } from './helpers/people'
4+
import { type Reactive } from 'vue'
5+
6+
describe('watchRecordStore() types', async () => {
7+
8+
it('type inferred types', async () => {
9+
const {
10+
people,
11+
getPerson,
12+
getPersonInfo,
13+
} = usePeople()
14+
15+
people.value.push({
16+
id: 99,
17+
name: 'Jim',
18+
})
19+
20+
people.value.push({
21+
id: 44,
22+
name: 'Sam',
23+
})
24+
const store = watchRecordStore(
25+
(id: number): Person => {
26+
return getPerson(id) as Person
27+
},
28+
(person, context): PersonInfo => {
29+
if (person.value.id !== 44) {
30+
const otherItem = context.get(44)
31+
expectTypeOf(otherItem).toEqualTypeOf<Reactive<PersonInfo>>()
32+
}
33+
34+
expectTypeOf<
35+
ReturnType<typeof context.get>
36+
>().toEqualTypeOf<Reactive<PersonInfo>>()
37+
38+
expectTypeOf<
39+
Parameters<typeof context.get>[0]
40+
>().toEqualTypeOf<number>()
41+
42+
return getPersonInfo(person)
43+
},
44+
)
45+
46+
const item = store.get(99)
47+
48+
expectTypeOf(item).toEqualTypeOf<Reactive<PersonInfo>>()
49+
50+
expectTypeOf<
51+
ReturnType<typeof store.get>
52+
>().toEqualTypeOf<Reactive<PersonInfo>>()
53+
54+
expectTypeOf<
55+
Parameters<typeof store.get>[0]
56+
>().toEqualTypeOf<number>()
57+
})
58+
})

0 commit comments

Comments
 (0)