Skip to content

Commit 40b279c

Browse files
committed
wip(types): mixins type support
1 parent 8c0aa34 commit 40b279c

File tree

6 files changed

+423
-264
lines changed

6 files changed

+423
-264
lines changed

types/index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ export {
4848
// v2 already has option with same name and it's for a single computed
4949
ComputedOptions as ComponentComputedOptions,
5050
MethodOptions as ComponentMethodOptions,
51-
ComponentPropsOptions
51+
ComponentPropsOptions,
52+
ComponentCustomOptions
5253
} from './v3-component-options'
5354
export {
5455
ComponentInstance,
5556
ComponentPublicInstance,
56-
ComponentRenderProxy
57-
} from './v3-component-proxy'
57+
CreateComponentPublicInstance,
58+
ComponentCustomProperties
59+
} from './v3-component-public-instance'
5860
export {
5961
// PropType,
6062
// PropOptions,

types/test/v3/define-component-test.tsx

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,42 +1035,6 @@ describe('extract instance type', () => {
10351035
expectError((compA.baseA = 1))
10361036
})
10371037

1038-
describe('async setup', () => {
1039-
type GT = string & { __brand: unknown }
1040-
const Comp = defineComponent({
1041-
async setup() {
1042-
// setup context
1043-
return {
1044-
a: ref(1),
1045-
b: {
1046-
c: ref('hi')
1047-
},
1048-
d: reactive({
1049-
e: ref('hello' as GT)
1050-
})
1051-
}
1052-
},
1053-
render() {
1054-
// assert setup context unwrapping
1055-
expectType<number>(this.a)
1056-
expectType<string>(this.b.c.value)
1057-
expectType<GT>(this.d.e)
1058-
1059-
// setup context properties should be mutable
1060-
this.a = 2
1061-
}
1062-
})
1063-
1064-
const vm = {} as InstanceType<typeof Comp>
1065-
// assert setup context unwrapping
1066-
expectType<number>(vm.a)
1067-
expectType<string>(vm.b.c.value)
1068-
expectType<GT>(vm.d.e)
1069-
1070-
// setup context properties should be mutable
1071-
vm.a = 2
1072-
})
1073-
10741038
// #5948
10751039
describe('DefineComponent should infer correct types when assigning to Component', () => {
10761040
let component: Component
@@ -1106,8 +1070,6 @@ describe('should allow to assign props', () => {
11061070

11071071
// check if defineComponent can be exported
11081072
export default {
1109-
// function components
1110-
a: defineComponent(_ => h('div')),
11111073
// no props
11121074
b: defineComponent({
11131075
data() {

types/v3-component-options.d.ts

Lines changed: 120 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,27 @@ import { ComponentOptions as Vue2ComponentOptions } from './options'
44
import { EmitsOptions, SetupContext } from './v3-setup-context'
55
import { Data } from './common'
66
import { ComponentPropsOptions, ExtractPropTypes } from './v3-component-props'
7-
import { ComponentRenderProxy } from './v3-component-proxy'
7+
import { CreateComponentPublicInstance } from './v3-component-public-instance'
88
export { ComponentPropsOptions } from './v3-component-props'
99

10+
/**
11+
* Interface for declaring custom options.
12+
*
13+
* @example
14+
* ```ts
15+
* declare module 'vue' {
16+
* interface ComponentCustomOptions {
17+
* beforeRouteUpdate?(
18+
* to: Route,
19+
* from: Route,
20+
* next: () => void
21+
* ): void
22+
* }
23+
* }
24+
* ```
25+
*/
26+
export interface ComponentCustomOptions {}
27+
1028
export type ComputedGetter<T> = (ctx?: any) => T
1129
export type ComputedSetter<T> = (v: T) => void
1230

@@ -34,24 +52,47 @@ export type SetupFunction<
3452
ctx: SetupContext<Emits>
3553
) => RawBindings | (() => VNode | null) | void
3654

37-
interface ComponentOptionsBase<
55+
export interface ComponentOptionsBase<
3856
Props,
39-
D = Data,
40-
C extends ComputedOptions = {},
41-
M extends MethodOptions = {}
57+
RawBindings,
58+
D,
59+
C extends ComputedOptions,
60+
M extends MethodOptions,
61+
Mixin extends ComponentOptionsMixin,
62+
Extends extends ComponentOptionsMixin,
63+
Emits extends EmitsOptions,
64+
EmitNames extends string = string
4265
> extends Omit<
43-
Vue2ComponentOptions<Vue, D, M, C, Props>,
44-
'data' | 'computed' | 'method' | 'setup' | 'props'
45-
> {
66+
Vue2ComponentOptions<Vue, D, M, C, Props>,
67+
'data' | 'computed' | 'method' | 'setup' | 'props' | 'mixins' | 'extends'
68+
>,
69+
ComponentCustomOptions {
4670
// allow any custom options
4771
[key: string]: any
4872

4973
// rewrite options api types
50-
data?: (this: Props & Vue, vm: Props) => D
74+
data?: (
75+
this: CreateComponentPublicInstance<Props, {}, {}, {}, M, Mixin, Extends>,
76+
vm: CreateComponentPublicInstance<Props, {}, {}, {}, M, Mixin, Extends>
77+
) => D
5178
computed?: C
52-
methods?: M
79+
mixins?: Mixin[]
80+
extends?: Extends
81+
emits?: (Emits | EmitNames[]) & ThisType<void>
82+
setup?: SetupFunction<Props, RawBindings, Emits>
5383
}
5484

85+
export type ComponentOptionsMixin = ComponentOptionsBase<
86+
any,
87+
any,
88+
any,
89+
any,
90+
any,
91+
any,
92+
any,
93+
any
94+
>
95+
5596
export type ExtractComputedReturns<T extends any> = {
5697
[key in keyof T]: T[key] extends { get: (...args: any[]) => infer TReturn }
5798
? TReturn
@@ -66,17 +107,34 @@ export type ComponentOptionsWithProps<
66107
D = Data,
67108
C extends ComputedOptions = {},
68109
M extends MethodOptions = {},
69-
Mixin = {},
70-
Extends = {},
110+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
111+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
71112
Emits extends EmitsOptions = {},
72113
EmitsNames extends string = string,
73114
Props = ExtractPropTypes<PropsOptions>
74-
> = ComponentOptionsBase<Props, D, C, M> & {
115+
> = ComponentOptionsBase<
116+
Props,
117+
RawBindings,
118+
D,
119+
C,
120+
M,
121+
Mixin,
122+
Extends,
123+
Emits,
124+
EmitsNames
125+
> & {
75126
props?: PropsOptions
76-
emits?: (Emits | EmitsNames[]) & ThisType<void>
77-
setup?: SetupFunction<Props, RawBindings, Emits>
78127
} & ThisType<
79-
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits>
128+
CreateComponentPublicInstance<
129+
Props,
130+
RawBindings,
131+
D,
132+
C,
133+
M,
134+
Mixin,
135+
Extends,
136+
Emits
137+
>
80138
>
81139

82140
export type ComponentOptionsWithArrayProps<
@@ -85,17 +143,34 @@ export type ComponentOptionsWithArrayProps<
85143
D = Data,
86144
C extends ComputedOptions = {},
87145
M extends MethodOptions = {},
88-
Mixin = {},
89-
Extends = {},
146+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
147+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
90148
Emits extends EmitsOptions = {},
91149
EmitsNames extends string = string,
92150
Props = Readonly<{ [key in PropNames]?: any }>
93-
> = ComponentOptionsBase<Props, D, C, M> & {
151+
> = ComponentOptionsBase<
152+
Props,
153+
RawBindings,
154+
D,
155+
C,
156+
M,
157+
Mixin,
158+
Extends,
159+
Emits,
160+
EmitsNames
161+
> & {
94162
props?: PropNames[]
95-
emits?: (Emits | EmitsNames[]) & ThisType<void>
96-
setup?: SetupFunction<Props, RawBindings, Emits>
97163
} & ThisType<
98-
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits>
164+
CreateComponentPublicInstance<
165+
Props,
166+
RawBindings,
167+
D,
168+
C,
169+
M,
170+
Mixin,
171+
Extends,
172+
Emits
173+
>
99174
>
100175

101176
export type ComponentOptionsWithoutProps<
@@ -104,16 +179,33 @@ export type ComponentOptionsWithoutProps<
104179
D = Data,
105180
C extends ComputedOptions = {},
106181
M extends MethodOptions = {},
107-
Mixin = {},
108-
Extends = {},
182+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
183+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
109184
Emits extends EmitsOptions = {},
110185
EmitsNames extends string = string
111-
> = ComponentOptionsBase<Props, D, C, M> & {
186+
> = ComponentOptionsBase<
187+
Props,
188+
RawBindings,
189+
D,
190+
C,
191+
M,
192+
Mixin,
193+
Extends,
194+
Emits,
195+
EmitsNames
196+
> & {
112197
props?: undefined
113-
emits?: (Emits | EmitsNames[]) & ThisType<void>
114-
setup?: SetupFunction<Props, RawBindings, Emits>
115198
} & ThisType<
116-
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits>
199+
CreateComponentPublicInstance<
200+
Props,
201+
RawBindings,
202+
D,
203+
C,
204+
M,
205+
Mixin,
206+
Extends,
207+
Emits
208+
>
117209
>
118210

119211
export type WithLegacyAPI<T, D, C, M, Props> = T &

0 commit comments

Comments
 (0)