Skip to content

Commit

Permalink
test: add dts tests for defineComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 23, 2020
1 parent 9047e4a commit 2d8b0cc
Show file tree
Hide file tree
Showing 5 changed files with 629 additions and 12 deletions.
8 changes: 2 additions & 6 deletions src/component/componentOptions.ts
@@ -1,11 +1,7 @@
import { Data } from './common'
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'
import { VNode } from 'vue'
import {
ComponentInstance,
VueProxy,
ComponentRenderProxy,
} from './componentProxy'
import { ComponentInstance, ComponentRenderProxy } from './componentProxy'

import { ComponentOptions as Vue2ComponentOptions } from 'vue'

Expand Down Expand Up @@ -52,7 +48,7 @@ interface ComponentOptionsBase<
Vue2ComponentOptions<Vue, D, M, C, Props>,
'data' | 'computed' | 'method' | 'setup' | 'props'
> {
data?: D | (() => D)
data?: (this: Props, vm: Props) => D
computed?: C
methods?: M
}
Expand Down
11 changes: 6 additions & 5 deletions src/component/componentProxy.ts
Expand Up @@ -25,18 +25,19 @@ export type ComponentRenderProxy<
PublicProps = P
> = {
$data: D
$props: P & PublicProps
$props: Readonly<P & PublicProps>
$attrs: Data
$refs: Data
$slots: Data
$root: ComponentInstance | null
$parent: ComponentInstance | null
$emit: (event: string, ...args: unknown[]) => void
} & P &
} & Readonly<P> &
UnwrapRef<B> &
D &
M &
ExtractComputedReturns<C>
ExtractComputedReturns<C> &
Vue

// for Vetur and TSX support
type VueConstructorProxy<PropsOptions, RawBindings> = VueConstructor & {
Expand All @@ -55,8 +56,8 @@ export type VueProxy<
PropsOptions,
RawBindings,
Data = DefaultData<Vue>,
Methods = DefaultMethods<Vue>,
Computed = DefaultComputed
Computed = DefaultComputed,
Methods = DefaultMethods<Vue>
> = Vue2ComponentOptions<
Vue,
UnwrapRef<RawBindings> & Data,
Expand Down
5 changes: 4 additions & 1 deletion src/component/defineComponent.ts
Expand Up @@ -8,6 +8,7 @@ import {
} from './componentOptions'
import { VueProxy } from './componentProxy'
import { Data } from './common'
import { HasDefined } from '../types/basic'

// overload 1: object format with no props
export function defineComponent<
Expand Down Expand Up @@ -43,7 +44,9 @@ export function defineComponent<
M extends MethodOptions = {},
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions
>(
options: ComponentOptionsWithProps<PropsOptions, RawBindings, D, C, M>
options: HasDefined<Props> extends true
? ComponentOptionsWithProps<PropsOptions, RawBindings, D, C, M, Props>
: ComponentOptionsWithProps<PropsOptions, RawBindings, D, C, M>
): VueProxy<PropsOptions, RawBindings, D, C, M>
// implementation, close to no-op
export function defineComponent(options: any) {
Expand Down

0 comments on commit 2d8b0cc

Please sign in to comment.