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(runtime-core): unwrap the RawBndings #2051

Merged
merged 2 commits into from
Sep 14, 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
11 changes: 6 additions & 5 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
ComponentOptionsWithArrayProps,
ComponentOptionsWithObjectProps,
ComponentOptionsMixin,
RenderFunction
RenderFunction,
UnwrapAsyncBindings
} from './componentOptions'
import {
SetupContext,
Expand Down Expand Up @@ -37,7 +38,7 @@ export function defineComponent<Props, RawBindings = object>(
): ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
RawBindings,
UnwrapAsyncBindings<RawBindings>,
{},
{},
{},
Expand Down Expand Up @@ -78,7 +79,7 @@ export function defineComponent<
): ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
RawBindings,
UnwrapAsyncBindings<RawBindings>,
D,
C,
M,
Expand Down Expand Up @@ -130,7 +131,7 @@ export function defineComponent<
// but now we can export array props in TSX
CreateComponentPublicInstance<
Readonly<{ [key in PropNames]?: any }>,
RawBindings,
UnwrapAsyncBindings<RawBindings>,
D,
C,
M,
Expand Down Expand Up @@ -181,7 +182,7 @@ export function defineComponent<
): ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
ExtractPropTypes<PropsOptions, false>,
RawBindings,
UnwrapAsyncBindings<RawBindings>,
D,
C,
M,
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export interface ComponentCustomOptions {}

export type RenderFunction = () => VNodeChild

export type UnwrapAsyncBindings<T> = T extends Promise<infer S> ? S : T

export interface ComponentOptionsBase<
Props,
RawBindings,
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
OptionTypesType,
OptionTypesKeys,
resolveMergedOptions,
isInBeforeCreate
isInBeforeCreate,
UnwrapAsyncBindings
} from './componentOptions'
import { EmitsOptions, EmitFn } from './componentEmits'
import { Slots } from './componentSlots'
Expand Down Expand Up @@ -168,7 +169,7 @@ export type ComponentPublicInstance<
options?: WatchOptions
): WatchStopHandle
} & P &
ShallowUnwrapRef<B> &
ShallowUnwrapRef<UnwrapAsyncBindings<B>> &
D &
ExtractComputedReturns<C> &
M &
Expand Down
36 changes: 36 additions & 0 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>(this.a)
expectType<string>(this.b.c.value)
expectType<GT>(this.d.e)

// setup context properties should be mutable
this.a = 2
}
})

const vm = {} as InstanceType<typeof Comp>
// assert setup context unwrapping
expectType<number>(vm.a)
expectType<string>(vm.b.c.value)
expectType<GT>(vm.d.e)

// setup context properties should be mutable
vm.a = 2
})