From aa568fe3222787641e18a90188689c9f5f0303d5 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Sat, 5 Sep 2020 00:02:48 +0800 Subject: [PATCH 1/2] types(runtime-core): unwrap the RawBndings --- .../runtime-core/src/apiDefineComponent.ts | 11 +++--- packages/runtime-core/src/componentOptions.ts | 2 ++ .../src/componentPublicInstance.ts | 5 +-- test-dts/defineComponent.test-d.tsx | 36 +++++++++++++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index eda62eaab03..fc4baeffb14 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -5,7 +5,8 @@ import { ComponentOptionsWithArrayProps, ComponentOptionsWithObjectProps, ComponentOptionsMixin, - RenderFunction + RenderFunction, + UnwrapRawBindings } from './componentOptions' import { SetupContext, @@ -37,7 +38,7 @@ export function defineComponent( ): ComponentPublicInstanceConstructor< CreateComponentPublicInstance< Props, - RawBindings, + UnwrapRawBindings, {}, {}, {}, @@ -78,7 +79,7 @@ export function defineComponent< ): ComponentPublicInstanceConstructor< CreateComponentPublicInstance< Props, - RawBindings, + UnwrapRawBindings, D, C, M, @@ -130,7 +131,7 @@ export function defineComponent< // but now we can export array props in TSX CreateComponentPublicInstance< Readonly<{ [key in PropNames]?: any }>, - RawBindings, + UnwrapRawBindings, D, C, M, @@ -181,7 +182,7 @@ export function defineComponent< ): ComponentPublicInstanceConstructor< CreateComponentPublicInstance< ExtractPropTypes, - RawBindings, + UnwrapRawBindings, D, C, M, diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index fd473714771..36dfe023980 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -72,6 +72,8 @@ export interface ComponentCustomOptions {} export type RenderFunction = () => VNodeChild +export type UnwrapRawBindings = T extends Promise ? S : T + export interface ComponentOptionsBase< Props, RawBindings, diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 695e5ace3c9..16444b050b9 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -27,7 +27,8 @@ import { OptionTypesType, OptionTypesKeys, resolveMergedOptions, - isInBeforeCreate + isInBeforeCreate, + UnwrapRawBindings } from './componentOptions' import { EmitsOptions, EmitFn } from './componentEmits' import { Slots } from './componentSlots' @@ -168,7 +169,7 @@ export type ComponentPublicInstance< options?: WatchOptions ): WatchStopHandle } & P & - ShallowUnwrapRef & + ShallowUnwrapRef> & D & ExtractComputedReturns & M & diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index 18c3338abeb..59d6a3bcb28 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -864,3 +864,39 @@ describe('extract instance type', () => { // @ts-expect-error expectError((compA.baseA = 1)) }) + +describe('async setup', () => { + type GT = string & { __brand: unknown } + const Comp = defineComponent({ + async setup() { + // setup context + return { + a: ref(1), + b: { + c: ref('hi') + }, + d: reactive({ + e: ref('hello' as GT) + }) + } + }, + render() { + // assert setup context unwrapping + expectType(this.a) + expectType(this.b.c.value) + expectType(this.d.e) + + // setup context properties should be mutable + this.a = 2 + } + }) + + const vm = {} as InstanceType + // assert setup context unwrapping + expectType(vm.a) + expectType(vm.b.c.value) + expectType(vm.d.e) + + // setup context properties should be mutable + vm.a = 2 +}) From 4e12501b6e3da31952090412dd1b51556a090514 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Sat, 5 Sep 2020 00:16:32 +0800 Subject: [PATCH 2/2] chore: rename --- packages/runtime-core/src/apiDefineComponent.ts | 10 +++++----- packages/runtime-core/src/componentOptions.ts | 2 +- packages/runtime-core/src/componentPublicInstance.ts | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index fc4baeffb14..c054e65a8eb 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -6,7 +6,7 @@ import { ComponentOptionsWithObjectProps, ComponentOptionsMixin, RenderFunction, - UnwrapRawBindings + UnwrapAsyncBindings } from './componentOptions' import { SetupContext, @@ -38,7 +38,7 @@ export function defineComponent( ): ComponentPublicInstanceConstructor< CreateComponentPublicInstance< Props, - UnwrapRawBindings, + UnwrapAsyncBindings, {}, {}, {}, @@ -79,7 +79,7 @@ export function defineComponent< ): ComponentPublicInstanceConstructor< CreateComponentPublicInstance< Props, - UnwrapRawBindings, + UnwrapAsyncBindings, D, C, M, @@ -131,7 +131,7 @@ export function defineComponent< // but now we can export array props in TSX CreateComponentPublicInstance< Readonly<{ [key in PropNames]?: any }>, - UnwrapRawBindings, + UnwrapAsyncBindings, D, C, M, @@ -182,7 +182,7 @@ export function defineComponent< ): ComponentPublicInstanceConstructor< CreateComponentPublicInstance< ExtractPropTypes, - UnwrapRawBindings, + UnwrapAsyncBindings, D, C, M, diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index 36dfe023980..04745551e66 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -72,7 +72,7 @@ export interface ComponentCustomOptions {} export type RenderFunction = () => VNodeChild -export type UnwrapRawBindings = T extends Promise ? S : T +export type UnwrapAsyncBindings = T extends Promise ? S : T export interface ComponentOptionsBase< Props, diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 16444b050b9..77fcbeeae26 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -28,7 +28,7 @@ import { OptionTypesKeys, resolveMergedOptions, isInBeforeCreate, - UnwrapRawBindings + UnwrapAsyncBindings } from './componentOptions' import { EmitsOptions, EmitFn } from './componentEmits' import { Slots } from './componentSlots' @@ -169,7 +169,7 @@ export type ComponentPublicInstance< options?: WatchOptions ): WatchStopHandle } & P & - ShallowUnwrapRef> & + ShallowUnwrapRef> & D & ExtractComputedReturns & M &