Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: add test for unwrapRef value and rollback Ref.isRefSymbol symbol #1115

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ describe('reactivity/reactive', () => {
expect(typeof obj.b).toBe(`number`)
})

it('should not unwrap interface with `value` property', () => {
const o = {
a: 1,
value: {
b: 2
}
}
const r = reactive(o)
expect(r).toMatchObject(o)
})

test('non-observable values', () => {
const assertValue = (value: any) => {
reactive(value)
Expand Down
12 changes: 12 additions & 0 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ import { reactive, isProxy, toRaw } from './reactive'
import { ComputedRef } from './computed'
import { CollectionTypes } from './collectionHandlers'

declare const isRefSymbol: unique symbol
export interface Ref<T = any> {
// This field is necessary to allow TS to differentiate a Ref from a plain
// object that happens to have a "value" field.
// However, checking a symbol on an arbitrary object is much slower than
// checking a plain property, so we use a __v_isRef plain property for isRef()
// check in the actual implementation.
// The reason for not just declaring __v_isRef in the interface is because we
// don't want this internal field to leak into userland autocompletion -
// a private symbol, on the other hand, achieves just that.
[isRefSymbol]: true

/**
* @internal
*/
__v_isRef: true

value: T
}

Expand Down
3 changes: 3 additions & 0 deletions test-dts/ref.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ function plainType(arg: number | Ref<number>) {
expectType<Ref<IteratorFoo | null | undefined>>(
ref<IteratorFoo | null | undefined>()
)

// with value
expect<Ref<{ value: { a: number } }>>(ref({ value: { a: 1 } }))
}

plainType(1)
Expand Down