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(reactivity): fix corner case on UnwrapRef #614

Merged
merged 1 commit into from
Jan 16, 2020
Merged
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
7 changes: 6 additions & 1 deletion packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ function toProxyRef<T extends object, K extends keyof T>(

type UnwrapArray<T> = { [P in keyof T]: UnwrapRef<T[P]> }

// corner case when use narrows type
// Ex. type RelativePath = string & { __brand: unknown }
// RelativePath extends object -> true
type BaseTypes = string | number | boolean

// Recursively unwraps nested value bindings.
export type UnwrapRef<T> = {
cRef: T extends ComputedRef<infer V> ? UnwrapRef<V> : T
Expand All @@ -97,6 +102,6 @@ export type UnwrapRef<T> = {
? 'ref'
: T extends Array<any>
? 'array'
: T extends Function | CollectionTypes
: T extends Function | CollectionTypes | BaseTypes
? 'ref' // bail out on types that shouldn't be unwrapped
: T extends object ? 'object' : 'ref']
6 changes: 6 additions & 0 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('with object props', () => {
ddd: string[]
}

type GT = string & { __brand: unknown }

const MyComponent = defineComponent({
props: {
a: Number,
Expand Down Expand Up @@ -57,6 +59,9 @@ describe('with object props', () => {
c: ref(1),
d: {
e: ref('hi')
},
f: {
g: ref('hello' as GT)
}
}
},
Expand Down Expand Up @@ -88,6 +93,7 @@ describe('with object props', () => {
// assert setup context unwrapping
expectType<number>(this.c)
expectType<string>(this.d.e)
expectType<GT>(this.f.g)

// setup context properties should be mutable
this.c = 2
Expand Down