From 2077eed7a01ec98a9493d6463f8d710957ad82ae Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Tue, 18 May 2021 13:16:26 +0100 Subject: [PATCH 1/4] types(markRaw): keep the original type when unwrapping `markRaw` --- packages/reactivity/src/reactive.ts | 6 ++++-- packages/reactivity/src/ref.ts | 21 +++++++++++-------- test-dts/reactivity.test-d.ts | 32 ++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/packages/reactivity/src/reactive.ts b/packages/reactivity/src/reactive.ts index ea800c83c61..e1a43fe4a2f 100644 --- a/packages/reactivity/src/reactive.ts +++ b/packages/reactivity/src/reactive.ts @@ -11,7 +11,7 @@ import { shallowCollectionHandlers, shallowReadonlyCollectionHandlers } from './collectionHandlers' -import { UnwrapRef, Ref } from './ref' +import { UnwrapRef, Ref, RAW_SYMBOL } from './ref' export const enum ReactiveFlags { SKIP = '__v_skip', @@ -230,7 +230,9 @@ export function toRaw(observed: T): T { ) } -export function markRaw(value: T): T { +export function markRaw( + value: T +): T & { [RAW_SYMBOL]?: true } { def(value, ReactiveFlags.SKIP, true) return value } diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 61ad4665275..8d50e8471ca 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -5,6 +5,7 @@ import { reactive, isProxy, toRaw, isReactive } from './reactive' import { CollectionTypes } from './collectionHandlers' declare const RefSymbol: unique symbol +export declare const RAW_SYMBOL: unique symbol export interface Ref { value: T @@ -212,16 +213,18 @@ export type UnwrapRef = T extends Ref ? UnwrapRefSimple : UnwrapRefSimple -type UnwrapRefSimple = T extends - | Function - | CollectionTypes - | BaseTypes - | Ref - | RefUnwrapBailTypes[keyof RefUnwrapBailTypes] +type UnwrapRefSimple = T extends { [RAW_SYMBOL]?: true } ? T - : T extends Array - ? { [K in keyof T]: UnwrapRefSimple } - : T extends object ? UnwrappedObject : T + : T extends + | Function + | CollectionTypes + | BaseTypes + | Ref + | RefUnwrapBailTypes[keyof RefUnwrapBailTypes] + ? T + : T extends Array + ? { [K in keyof T]: UnwrapRefSimple } + : T extends object ? UnwrappedObject : T // Extract all known symbols from an object // when unwrapping Object the symbols are not `in keyof`, this should cover all the diff --git a/test-dts/reactivity.test-d.ts b/test-dts/reactivity.test-d.ts index edccd4fa099..4522d3ec8dd 100644 --- a/test-dts/reactivity.test-d.ts +++ b/test-dts/reactivity.test-d.ts @@ -1,4 +1,5 @@ -import { readonly, describe, expectError } from './index' +import { reactive, Ref, ref } from '@vue/reactivity' +import { readonly, describe, expectError, markRaw, expectType } from './index' describe('should support DeepReadonly', () => { const r = readonly({ obj: { k: 'v' } }) @@ -7,3 +8,32 @@ describe('should support DeepReadonly', () => { // @ts-expect-error expectError((r.obj.k = 'x')) }) + +describe('should support markRaw', () => { + class Test { + item = {} as T + } + const test = new Test() + const plain = { + ref: ref(1) + } + + const r = reactive({ + class: { + raw: markRaw(test), + reactive: test + }, + plain: { + raw: markRaw(plain), + reactive: plain + } + }) + + expectType>(r.class.raw) + // TODO make this fail + expectType>(r.class.reactive) + + expectType>(r.plain.raw.ref) + // @ts-expect-error it should unwrap + expectType>(r.plain.reactive.ref) +}) From cb8a25b51bbd5288d038a97678e12785331a014d Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Tue, 18 May 2021 13:20:30 +0100 Subject: [PATCH 2/4] chore: fix import --- test-dts/reactivity.test-d.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/test-dts/reactivity.test-d.ts b/test-dts/reactivity.test-d.ts index 4522d3ec8dd..000349bd946 100644 --- a/test-dts/reactivity.test-d.ts +++ b/test-dts/reactivity.test-d.ts @@ -1,5 +1,13 @@ -import { reactive, Ref, ref } from '@vue/reactivity' -import { readonly, describe, expectError, markRaw, expectType } from './index' +import { + readonly, + describe, + expectError, + markRaw, + expectType, + reactive, + Ref, + ref +} from './index' describe('should support DeepReadonly', () => { const r = readonly({ obj: { k: 'v' } }) @@ -11,7 +19,7 @@ describe('should support DeepReadonly', () => { describe('should support markRaw', () => { class Test { - item = {} as T + item = {} as Ref } const test = new Test() const plain = { @@ -30,7 +38,7 @@ describe('should support markRaw', () => { }) expectType>(r.class.raw) - // TODO make this fail + // @ts-expect-error it should unwrap expectType>(r.class.reactive) expectType>(r.plain.raw.ref) From e91914745819c35e75ebfca597bf0dfee8f9f53c Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Tue, 25 May 2021 06:15:18 +0100 Subject: [PATCH 3/4] chore: rename to RawSymbol to match RefSymbol casing --- packages/reactivity/src/reactive.ts | 4 ++-- packages/reactivity/src/ref.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/reactivity/src/reactive.ts b/packages/reactivity/src/reactive.ts index e1a43fe4a2f..1f2f737f62b 100644 --- a/packages/reactivity/src/reactive.ts +++ b/packages/reactivity/src/reactive.ts @@ -11,7 +11,7 @@ import { shallowCollectionHandlers, shallowReadonlyCollectionHandlers } from './collectionHandlers' -import { UnwrapRef, Ref, RAW_SYMBOL } from './ref' +import { UnwrapRef, Ref, RawSymbol } from './ref' export const enum ReactiveFlags { SKIP = '__v_skip', @@ -232,7 +232,7 @@ export function toRaw(observed: T): T { export function markRaw( value: T -): T & { [RAW_SYMBOL]?: true } { +): T & { [RawSymbol]?: true } { def(value, ReactiveFlags.SKIP, true) return value } diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 351b7498f63..c7c808b43f9 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -5,7 +5,7 @@ import { reactive, isProxy, toRaw, isReactive } from './reactive' import { CollectionTypes } from './collectionHandlers' export declare const RefSymbol: unique symbol -export declare const RAW_SYMBOL: unique symbol +export declare const RawSymbol: unique symbol export interface Ref { value: T @@ -213,7 +213,7 @@ export type UnwrapRef = T extends Ref ? UnwrapRefSimple : UnwrapRefSimple -type UnwrapRefSimple = T extends { [RAW_SYMBOL]?: true } +type UnwrapRefSimple = T extends { [RawSymbol]?: true } ? T : T extends | Function From 40ae22263985a2550fc0c1c4694e74267890c699 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 6 May 2022 05:01:53 -0400 Subject: [PATCH 4/4] Update ref.ts --- packages/reactivity/src/ref.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 5297f95649e..2a3b3732e0e 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -12,7 +12,7 @@ import { CollectionTypes } from './collectionHandlers' import { createDep, Dep } from './dep' declare const RefSymbol: unique symbol -declare const RawSymbol: unique symbol +export declare const RawSymbol: unique symbol export interface Ref { value: T