From: IU Date: Thu, 16 Jan 2020 22:47:47 +0000 (+0800) Subject: types(reactivity): handle primitive + object intersection types in UnwrapRef (#614) X-Git-Tag: v3.0.0-alpha.3~19 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2b4d0d6501e22b6c99f1375df669ebb622187186;p=thirdparty%2Fvuejs%2Fcore.git types(reactivity): handle primitive + object intersection types in UnwrapRef (#614) --- diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index b4127c1130..10e577fe5f 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -85,6 +85,11 @@ function toProxyRef( type UnwrapArray = { [P in keyof T]: UnwrapRef } +// 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 = { cRef: T extends ComputedRef ? UnwrapRef : T @@ -97,6 +102,6 @@ export type UnwrapRef = { ? 'ref' : T extends Array ? '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'] diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index 3cde7426e9..a6393dd15b 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -12,6 +12,8 @@ describe('with object props', () => { ddd: string[] } + type GT = string & { __brand: unknown } + const MyComponent = defineComponent({ props: { a: Number, @@ -57,6 +59,9 @@ describe('with object props', () => { c: ref(1), d: { e: ref('hi') + }, + f: { + g: ref('hello' as GT) } } }, @@ -88,6 +93,7 @@ describe('with object props', () => { // assert setup context unwrapping expectType(this.c) expectType(this.d.e) + expectType(this.f.g) // setup context properties should be mutable this.c = 2