Skip to content

Commit 32e53bf

Browse files
authored
fix(types): keep the original type when unwrapping markRaw (#3791)
1 parent 67099fe commit 32e53bf

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

Diff for: packages/reactivity/src/reactive.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
shallowCollectionHandlers,
1212
shallowReadonlyCollectionHandlers
1313
} from './collectionHandlers'
14-
import { UnwrapRefSimple, Ref } from './ref'
14+
import { UnwrapRefSimple, Ref, RawSymbol } from './ref'
1515

1616
export const enum ReactiveFlags {
1717
SKIP = '__v_skip',
@@ -241,7 +241,9 @@ export function toRaw<T>(observed: T): T {
241241
return raw ? toRaw(raw) : observed
242242
}
243243

244-
export function markRaw<T extends object>(value: T): T {
244+
export function markRaw<T extends object>(
245+
value: T
246+
): T & { [RawSymbol]?: true } {
245247
def(value, ReactiveFlags.SKIP, true)
246248
return value
247249
}

Diff for: packages/reactivity/src/ref.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { CollectionTypes } from './collectionHandlers'
1212
import { createDep, Dep } from './dep'
1313

1414
declare const RefSymbol: unique symbol
15+
export declare const RawSymbol: unique symbol
1516

1617
export interface Ref<T = any> {
1718
value: T
@@ -291,6 +292,7 @@ export type UnwrapRefSimple<T> = T extends
291292
| BaseTypes
292293
| Ref
293294
| RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
295+
| { [RawSymbol]?: true }
294296
? T
295297
: T extends Array<any>
296298
? { [K in keyof T]: UnwrapRefSimple<T[K]> }

Diff for: test-dts/index.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export function expectType<T>(value: T): void
99
export function expectError<T>(value: T): void
1010
export function expectAssignable<T, T2 extends T = T>(value: T2): void
1111

12-
export type IsUnion<T, U extends T = T> = (T extends any
13-
? (U extends T ? false : true)
14-
: never) extends false
12+
export type IsUnion<T, U extends T = T> = (
13+
T extends any ? (U extends T ? false : true) : never
14+
) extends false
1515
? false
1616
: true
1717

Diff for: test-dts/reactivity.test-d.ts

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
import { shallowReadonly } from '@vue/reactivity'
2-
import { ref, readonly, describe, expectError, expectType, Ref } from './index'
1+
import {
2+
ref,
3+
readonly,
4+
shallowReadonly,
5+
describe,
6+
expectError,
7+
expectType,
8+
Ref,
9+
reactive,
10+
markRaw
11+
} from './index'
312

413
describe('should support DeepReadonly', () => {
514
const r = readonly({ obj: { k: 'v' } })
@@ -15,6 +24,35 @@ describe('readonly ref', () => {
1524
expectType<Ref>(r)
1625
})
1726

27+
describe('should support markRaw', () => {
28+
class Test<T> {
29+
item = {} as Ref<T>
30+
}
31+
const test = new Test<number>()
32+
const plain = {
33+
ref: ref(1)
34+
}
35+
36+
const r = reactive({
37+
class: {
38+
raw: markRaw(test),
39+
reactive: test
40+
},
41+
plain: {
42+
raw: markRaw(plain),
43+
reactive: plain
44+
}
45+
})
46+
47+
expectType<Test<number>>(r.class.raw)
48+
// @ts-expect-error it should unwrap
49+
expectType<Test<number>>(r.class.reactive)
50+
51+
expectType<Ref<number>>(r.plain.raw.ref)
52+
// @ts-expect-error it should unwrap
53+
expectType<Ref<number>>(r.plain.reactive.ref)
54+
})
55+
1856
describe('shallowReadonly ref unwrap', () => {
1957
const r = shallowReadonly({ count: { n: ref(1) } })
2058
// @ts-expect-error

0 commit comments

Comments
 (0)