Skip to content

Commit 2d8b0cc

Browse files
committed
test: add dts tests for defineComponent
1 parent 9047e4a commit 2d8b0cc

File tree

5 files changed

+629
-12
lines changed

5 files changed

+629
-12
lines changed

src/component/componentOptions.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { Data } from './common'
22
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'
33
import { VNode } from 'vue'
4-
import {
5-
ComponentInstance,
6-
VueProxy,
7-
ComponentRenderProxy,
8-
} from './componentProxy'
4+
import { ComponentInstance, ComponentRenderProxy } from './componentProxy'
95

106
import { ComponentOptions as Vue2ComponentOptions } from 'vue'
117

@@ -52,7 +48,7 @@ interface ComponentOptionsBase<
5248
Vue2ComponentOptions<Vue, D, M, C, Props>,
5349
'data' | 'computed' | 'method' | 'setup' | 'props'
5450
> {
55-
data?: D | (() => D)
51+
data?: (this: Props, vm: Props) => D
5652
computed?: C
5753
methods?: M
5854
}

src/component/componentProxy.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ export type ComponentRenderProxy<
2525
PublicProps = P
2626
> = {
2727
$data: D
28-
$props: P & PublicProps
28+
$props: Readonly<P & PublicProps>
2929
$attrs: Data
3030
$refs: Data
3131
$slots: Data
3232
$root: ComponentInstance | null
3333
$parent: ComponentInstance | null
3434
$emit: (event: string, ...args: unknown[]) => void
35-
} & P &
35+
} & Readonly<P> &
3636
UnwrapRef<B> &
3737
D &
3838
M &
39-
ExtractComputedReturns<C>
39+
ExtractComputedReturns<C> &
40+
Vue
4041

4142
// for Vetur and TSX support
4243
type VueConstructorProxy<PropsOptions, RawBindings> = VueConstructor & {
@@ -55,8 +56,8 @@ export type VueProxy<
5556
PropsOptions,
5657
RawBindings,
5758
Data = DefaultData<Vue>,
58-
Methods = DefaultMethods<Vue>,
59-
Computed = DefaultComputed
59+
Computed = DefaultComputed,
60+
Methods = DefaultMethods<Vue>
6061
> = Vue2ComponentOptions<
6162
Vue,
6263
UnwrapRef<RawBindings> & Data,

src/component/defineComponent.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from './componentOptions'
99
import { VueProxy } from './componentProxy'
1010
import { Data } from './common'
11+
import { HasDefined } from '../types/basic'
1112

1213
// overload 1: object format with no props
1314
export function defineComponent<
@@ -43,7 +44,9 @@ export function defineComponent<
4344
M extends MethodOptions = {},
4445
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions
4546
>(
46-
options: ComponentOptionsWithProps<PropsOptions, RawBindings, D, C, M>
47+
options: HasDefined<Props> extends true
48+
? ComponentOptionsWithProps<PropsOptions, RawBindings, D, C, M, Props>
49+
: ComponentOptionsWithProps<PropsOptions, RawBindings, D, C, M>
4750
): VueProxy<PropsOptions, RawBindings, D, C, M>
4851
// implementation, close to no-op
4952
export function defineComponent(options: any) {

0 commit comments

Comments
 (0)