fix(types): properly infer return type from async setup (#2051)
fix #2049
This commit is contained in:
parent
0124eacc91
commit
24fcf6ae7c
@ -5,7 +5,8 @@ import {
|
|||||||
ComponentOptionsWithArrayProps,
|
ComponentOptionsWithArrayProps,
|
||||||
ComponentOptionsWithObjectProps,
|
ComponentOptionsWithObjectProps,
|
||||||
ComponentOptionsMixin,
|
ComponentOptionsMixin,
|
||||||
RenderFunction
|
RenderFunction,
|
||||||
|
UnwrapAsyncBindings
|
||||||
} from './componentOptions'
|
} from './componentOptions'
|
||||||
import {
|
import {
|
||||||
SetupContext,
|
SetupContext,
|
||||||
@ -37,7 +38,7 @@ export function defineComponent<Props, RawBindings = object>(
|
|||||||
): ComponentPublicInstanceConstructor<
|
): ComponentPublicInstanceConstructor<
|
||||||
CreateComponentPublicInstance<
|
CreateComponentPublicInstance<
|
||||||
Props,
|
Props,
|
||||||
RawBindings,
|
UnwrapAsyncBindings<RawBindings>,
|
||||||
{},
|
{},
|
||||||
{},
|
{},
|
||||||
{},
|
{},
|
||||||
@ -78,7 +79,7 @@ export function defineComponent<
|
|||||||
): ComponentPublicInstanceConstructor<
|
): ComponentPublicInstanceConstructor<
|
||||||
CreateComponentPublicInstance<
|
CreateComponentPublicInstance<
|
||||||
Props,
|
Props,
|
||||||
RawBindings,
|
UnwrapAsyncBindings<RawBindings>,
|
||||||
D,
|
D,
|
||||||
C,
|
C,
|
||||||
M,
|
M,
|
||||||
@ -130,7 +131,7 @@ export function defineComponent<
|
|||||||
// but now we can export array props in TSX
|
// but now we can export array props in TSX
|
||||||
CreateComponentPublicInstance<
|
CreateComponentPublicInstance<
|
||||||
Readonly<{ [key in PropNames]?: any }>,
|
Readonly<{ [key in PropNames]?: any }>,
|
||||||
RawBindings,
|
UnwrapAsyncBindings<RawBindings>,
|
||||||
D,
|
D,
|
||||||
C,
|
C,
|
||||||
M,
|
M,
|
||||||
@ -181,7 +182,7 @@ export function defineComponent<
|
|||||||
): ComponentPublicInstanceConstructor<
|
): ComponentPublicInstanceConstructor<
|
||||||
CreateComponentPublicInstance<
|
CreateComponentPublicInstance<
|
||||||
ExtractPropTypes<PropsOptions, false>,
|
ExtractPropTypes<PropsOptions, false>,
|
||||||
RawBindings,
|
UnwrapAsyncBindings<RawBindings>,
|
||||||
D,
|
D,
|
||||||
C,
|
C,
|
||||||
M,
|
M,
|
||||||
|
@ -72,6 +72,8 @@ export interface ComponentCustomOptions {}
|
|||||||
|
|
||||||
export type RenderFunction = () => VNodeChild
|
export type RenderFunction = () => VNodeChild
|
||||||
|
|
||||||
|
export type UnwrapAsyncBindings<T> = T extends Promise<infer S> ? S : T
|
||||||
|
|
||||||
export interface ComponentOptionsBase<
|
export interface ComponentOptionsBase<
|
||||||
Props,
|
Props,
|
||||||
RawBindings,
|
RawBindings,
|
||||||
|
@ -27,7 +27,8 @@ import {
|
|||||||
OptionTypesType,
|
OptionTypesType,
|
||||||
OptionTypesKeys,
|
OptionTypesKeys,
|
||||||
resolveMergedOptions,
|
resolveMergedOptions,
|
||||||
isInBeforeCreate
|
isInBeforeCreate,
|
||||||
|
UnwrapAsyncBindings
|
||||||
} from './componentOptions'
|
} from './componentOptions'
|
||||||
import { EmitsOptions, EmitFn } from './componentEmits'
|
import { EmitsOptions, EmitFn } from './componentEmits'
|
||||||
import { Slots } from './componentSlots'
|
import { Slots } from './componentSlots'
|
||||||
@ -168,7 +169,7 @@ export type ComponentPublicInstance<
|
|||||||
options?: WatchOptions
|
options?: WatchOptions
|
||||||
): WatchStopHandle
|
): WatchStopHandle
|
||||||
} & P &
|
} & P &
|
||||||
ShallowUnwrapRef<B> &
|
ShallowUnwrapRef<UnwrapAsyncBindings<B>> &
|
||||||
D &
|
D &
|
||||||
ExtractComputedReturns<C> &
|
ExtractComputedReturns<C> &
|
||||||
M &
|
M &
|
||||||
|
@ -864,3 +864,39 @@ describe('extract instance type', () => {
|
|||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
expectError((compA.baseA = 1))
|
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
|
||||||
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user