Skip to content

Commit 9dd2d93

Browse files
committed
fix: 修复当使用markRaw时类型推导出错的问题
1 parent c31bc20 commit 9dd2d93

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

packages/core/__tests__/reactive.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, vi, test } from 'vitest';
1+
import { describe, expect, test } from 'vitest';
22
import { AutoStore, isRaw,markRaw } from '../src';
33

44
// 测试 isEventMatched 函数
@@ -8,12 +8,15 @@ describe("reactive",()=>{
88
a:1,
99
b:2,
1010
c:3,
11+
total:async ()=>store.state.a+store.state.b+store.state.c,
1112
x:markRaw({
13+
total:async ()=>store.state.a+store.state.b+store.state.c,
1214
x1:1,
1315
x2:2,
1416
x3:{
1517
x31:1,
16-
x32:2
18+
x32:2,
19+
x33: ()=>1
1720
}
1821
})
1922
})
@@ -26,5 +29,8 @@ describe("reactive",()=>{
2629
store.state.x.x2
2730
store.state.x.x3.x31
2831
store.state.x.x3.x32
32+
store.state.x.x3.x33
33+
store.state.x.total
34+
store.state.total
2935
})
3036
})

packages/core/src/types.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AsyncComputedDescriptorBuilder, AsyncComputedGetter, AsyncComputedValue, ComputedGetter, SyncComputedDescriptorBuilder } from "./computed";
22
import type { AutoStore } from "./store";
3+
import { RawObject } from "./utils";
34
import { WatchDescriptorBuilder } from "./watch/types";
45
import { Get,Paths} from "type-fest"
56

@@ -31,21 +32,23 @@ export type PickComputedResult<T> = T extends AsyncComputedDescriptorBuilder<inf
3132

3233

3334
export type ComputedState<T> = T extends unknown[] ? ComputedState<T[number]>[]
34-
: (
35-
T extends (...args:any) => any ? PickComputedResult<T>
35+
:
36+
T extends RawObject<T> ? T
3637
: (
37-
T extends Dict ? {
38-
[K in keyof T]: T[K] extends (...args:any[]) => any ? PickComputedResult<T[K]>
39-
: (
40-
T[K] extends Record<string, any> ? ComputedState<T[K]>
41-
: (
42-
T[K] extends unknown[] ? ComputedState<T[K][number]>[] : T[K]
43-
)
44-
)
45-
}
46-
: T
47-
)
48-
38+
T extends (...args:any) => any ? PickComputedResult<T>
39+
: (
40+
T extends Dict ? {
41+
[K in keyof T]: T[K] extends (...args:any[]) => any ? PickComputedResult<T[K]>
42+
: (
43+
T[K] extends Record<string, any> ? ComputedState<T[K]>
44+
: (
45+
T[K] extends unknown[] ? ComputedState<T[K][number]>[] : T[K]
46+
)
47+
)
48+
}
49+
: T
50+
)
51+
)
4952
)
5053

5154
// export type ComputedStateBak<T extends Record<string, any>> = {

packages/core/src/utils/markRaw.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { SKIP_PROXY_FLAG } from "../consts";
22

3+
export type RawObject<T> = T & {
4+
[SKIP_PROXY_FLAG]: true
5+
}
36

47
/**
58
* 标记一个对象为非响应式,不创建响应式代理
69
*
710
* @param obj
811
* @returns
912
*/
10-
export function markRaw(obj: any) {
13+
export function markRaw<T=any>(obj: T):RawObject<T>{
1114
try{
15+
// @ts-ignore
1216
obj[SKIP_PROXY_FLAG] = true
1317
}catch{}
14-
return obj;
18+
return obj as RawObject<T>
1519
}

0 commit comments

Comments
 (0)