Skip to content

Commit e4de623

Browse files
authored
fix(types): support inferring injected properties in options api (#6804)
close #3031 close #5931
1 parent 50e2253 commit e4de623

File tree

7 files changed

+222
-33
lines changed

7 files changed

+222
-33
lines changed

packages/runtime-core/src/apiDefineComponent.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
ComponentOptionsWithObjectProps,
77
ComponentOptionsMixin,
88
RenderFunction,
9-
ComponentOptionsBase
9+
ComponentOptionsBase,
10+
ComponentInjectOptions
1011
} from './componentOptions'
1112
import {
1213
SetupContext,
@@ -104,7 +105,9 @@ export function defineComponent<
104105
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
105106
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
106107
E extends EmitsOptions = {},
107-
EE extends string = string
108+
EE extends string = string,
109+
I extends ComponentInjectOptions = {},
110+
II extends string = string
108111
>(
109112
options: ComponentOptionsWithoutProps<
110113
Props,
@@ -115,7 +118,9 @@ export function defineComponent<
115118
Mixin,
116119
Extends,
117120
E,
118-
EE
121+
EE,
122+
I,
123+
II
119124
>
120125
): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE>
121126

@@ -131,7 +136,9 @@ export function defineComponent<
131136
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
132137
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
133138
E extends EmitsOptions = {},
134-
EE extends string = string
139+
EE extends string = string,
140+
I extends ComponentInjectOptions = {},
141+
II extends string = string,
135142
>(
136143
options: ComponentOptionsWithArrayProps<
137144
PropNames,
@@ -142,7 +149,9 @@ export function defineComponent<
142149
Mixin,
143150
Extends,
144151
E,
145-
EE
152+
EE,
153+
I,
154+
II
146155
>
147156
): DefineComponent<
148157
Readonly<{ [key in PropNames]?: any }>,
@@ -169,7 +178,9 @@ export function defineComponent<
169178
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
170179
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
171180
E extends EmitsOptions = {},
172-
EE extends string = string
181+
EE extends string = string,
182+
I extends ComponentInjectOptions = {},
183+
II extends string = string,
173184
>(
174185
options: ComponentOptionsWithObjectProps<
175186
PropsOptions,
@@ -180,7 +191,9 @@ export function defineComponent<
180191
Mixin,
181192
Extends,
182193
E,
183-
EE
194+
EE,
195+
I,
196+
II
184197
>
185198
): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>
186199

packages/runtime-core/src/componentOptions.ts

+44-13
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ export interface ComponentOptionsBase<
118118
Extends extends ComponentOptionsMixin,
119119
E extends EmitsOptions,
120120
EE extends string = string,
121-
Defaults = {}
122-
> extends LegacyOptions<Props, D, C, M, Mixin, Extends>,
121+
Defaults = {},
122+
I extends ComponentInjectOptions = {},
123+
II extends string = string
124+
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, I, II>,
123125
ComponentInternalOptions,
124126
ComponentCustomOptions {
125127
setup?: (
@@ -225,7 +227,9 @@ export type ComponentOptionsWithoutProps<
225227
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
226228
E extends EmitsOptions = EmitsOptions,
227229
EE extends string = string,
228-
PE = Props & EmitsToProps<E>
230+
I extends ComponentInjectOptions = {},
231+
II extends string = string,
232+
PE = Props & EmitsToProps<E>,
229233
> = ComponentOptionsBase<
230234
PE,
231235
RawBindings,
@@ -236,11 +240,13 @@ export type ComponentOptionsWithoutProps<
236240
Extends,
237241
E,
238242
EE,
239-
{}
243+
{},
244+
I,
245+
II
240246
> & {
241247
props?: undefined
242248
} & ThisType<
243-
CreateComponentPublicInstance<PE, RawBindings, D, C, M, Mixin, Extends, E>
249+
CreateComponentPublicInstance<PE, RawBindings, D, C, M, Mixin, Extends, E, PE, {}, false, I>
244250
>
245251

246252
export type ComponentOptionsWithArrayProps<
@@ -253,6 +259,8 @@ export type ComponentOptionsWithArrayProps<
253259
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
254260
E extends EmitsOptions = EmitsOptions,
255261
EE extends string = string,
262+
I extends ComponentInjectOptions = {},
263+
II extends string = string,
256264
Props = Readonly<{ [key in PropNames]?: any }> & EmitsToProps<E>
257265
> = ComponentOptionsBase<
258266
Props,
@@ -264,7 +272,9 @@ export type ComponentOptionsWithArrayProps<
264272
Extends,
265273
E,
266274
EE,
267-
{}
275+
{},
276+
I,
277+
II
268278
> & {
269279
props: PropNames[]
270280
} & ThisType<
@@ -276,7 +286,11 @@ export type ComponentOptionsWithArrayProps<
276286
M,
277287
Mixin,
278288
Extends,
279-
E
289+
E,
290+
Props,
291+
{},
292+
false,
293+
I
280294
>
281295
>
282296

@@ -290,8 +304,10 @@ export type ComponentOptionsWithObjectProps<
290304
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
291305
E extends EmitsOptions = EmitsOptions,
292306
EE extends string = string,
307+
I extends ComponentInjectOptions = {},
308+
II extends string = string,
293309
Props = Readonly<ExtractPropTypes<PropsOptions>> & EmitsToProps<E>,
294-
Defaults = ExtractDefaultPropTypes<PropsOptions>
310+
Defaults = ExtractDefaultPropTypes<PropsOptions>,
295311
> = ComponentOptionsBase<
296312
Props,
297313
RawBindings,
@@ -302,7 +318,9 @@ export type ComponentOptionsWithObjectProps<
302318
Extends,
303319
E,
304320
EE,
305-
Defaults
321+
Defaults,
322+
I,
323+
II
306324
> & {
307325
props: PropsOptions & ThisType<void>
308326
} & ThisType<
@@ -317,7 +335,8 @@ export type ComponentOptionsWithObjectProps<
317335
E,
318336
Props,
319337
Defaults,
320-
false
338+
false,
339+
I
321340
>
322341
>
323342

@@ -389,20 +408,32 @@ export type ComponentProvideOptions = ObjectProvideOptions | Function
389408

390409
type ObjectProvideOptions = Record<string | symbol, unknown>
391410

392-
type ComponentInjectOptions = string[] | ObjectInjectOptions
411+
export type ComponentInjectOptions = string[] | ObjectInjectOptions
393412

394413
type ObjectInjectOptions = Record<
395414
string | symbol,
396415
string | symbol | { from?: string | symbol; default?: unknown }
397416
>
398417

418+
export type InjectToObject<T extends ComponentInjectOptions> = T extends string[]
419+
? {
420+
[K in T[number]]?: unknown
421+
}
422+
: T extends ObjectInjectOptions
423+
? {
424+
[K in keyof T]?: unknown
425+
}
426+
: never
427+
399428
interface LegacyOptions<
400429
Props,
401430
D,
402431
C extends ComputedOptions,
403432
M extends MethodOptions,
404433
Mixin extends ComponentOptionsMixin,
405-
Extends extends ComponentOptionsMixin
434+
Extends extends ComponentOptionsMixin,
435+
I extends ComponentInjectOptions,
436+
II extends string
406437
> {
407438
compatConfig?: CompatConfig
408439

@@ -437,7 +468,7 @@ interface LegacyOptions<
437468
methods?: M
438469
watch?: ComponentWatchOptions
439470
provide?: ComponentProvideOptions
440-
inject?: ComponentInjectOptions
471+
inject?: I | II[]
441472

442473
// assets
443474
filters?: Record<string, Function>

packages/runtime-core/src/componentPublicInstance.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ import {
3434
OptionTypesKeys,
3535
resolveMergedOptions,
3636
shouldCacheAccess,
37-
MergedComponentOptionsOverride
37+
MergedComponentOptionsOverride,
38+
InjectToObject,
39+
ComponentInjectOptions
3840
} from './componentOptions'
3941
import { EmitsOptions, EmitFn } from './componentEmits'
4042
import { Slots } from './componentSlots'
@@ -141,6 +143,7 @@ export type CreateComponentPublicInstance<
141143
PublicProps = P,
142144
Defaults = {},
143145
MakeDefaultsOptional extends boolean = false,
146+
I extends ComponentInjectOptions = {},
144147
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
145148
PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
146149
PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
@@ -150,7 +153,7 @@ export type CreateComponentPublicInstance<
150153
PublicM extends MethodOptions = UnwrapMixinsType<PublicMixin, 'M'> &
151154
EnsureNonVoid<M>,
152155
PublicDefaults = UnwrapMixinsType<PublicMixin, 'Defaults'> &
153-
EnsureNonVoid<Defaults>
156+
EnsureNonVoid<Defaults>,
154157
> = ComponentPublicInstance<
155158
PublicP,
156159
PublicB,
@@ -161,7 +164,8 @@ export type CreateComponentPublicInstance<
161164
PublicProps,
162165
PublicDefaults,
163166
MakeDefaultsOptional,
164-
ComponentOptionsBase<P, B, D, C, M, Mixin, Extends, E, string, Defaults>
167+
ComponentOptionsBase<P, B, D, C, M, Mixin, Extends, E, string, Defaults>,
168+
I
165169
>
166170

167171
// public properties exposed on the proxy, which is used as the render context
@@ -176,7 +180,8 @@ export type ComponentPublicInstance<
176180
PublicProps = P,
177181
Defaults = {},
178182
MakeDefaultsOptional extends boolean = false,
179-
Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>
183+
Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>,
184+
I extends ComponentInjectOptions = {}
180185
> = {
181186
$: ComponentInternalInstance
182187
$data: D
@@ -205,7 +210,8 @@ export type ComponentPublicInstance<
205210
UnwrapNestedRefs<D> &
206211
ExtractComputedReturns<C> &
207212
M &
208-
ComponentCustomProperties
213+
ComponentCustomProperties &
214+
InjectToObject<I>
209215

210216
export type PublicPropertiesMap = Record<
211217
string,

packages/runtime-core/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ export {
222222
RenderFunction,
223223
MethodOptions,
224224
ComputedOptions,
225-
RuntimeCompilerOptions
225+
RuntimeCompilerOptions,
226+
ComponentInjectOptions
226227
} from './componentOptions'
227228
export { EmitsOptions, ObjectEmitsOptions } from './componentEmits'
228229
export {

packages/runtime-dom/src/apiCustomElement.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
nextTick,
2020
warn,
2121
ConcreteComponent,
22-
ComponentOptions
22+
ComponentOptions,
23+
ComponentInjectOptions
2324
} from '@vue/runtime-core'
2425
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
2526
import { hydrate, render } from '.'
@@ -49,7 +50,9 @@ export function defineCustomElement<
4950
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
5051
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
5152
E extends EmitsOptions = EmitsOptions,
52-
EE extends string = string
53+
EE extends string = string,
54+
I extends ComponentInjectOptions = {},
55+
II extends string = string
5356
>(
5457
options: ComponentOptionsWithoutProps<
5558
Props,
@@ -60,7 +63,9 @@ export function defineCustomElement<
6063
Mixin,
6164
Extends,
6265
E,
63-
EE
66+
EE,
67+
I,
68+
II
6469
> & { styles?: string[] }
6570
): VueElementConstructor<Props>
6671

@@ -74,7 +79,9 @@ export function defineCustomElement<
7479
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
7580
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
7681
E extends EmitsOptions = Record<string, any>,
77-
EE extends string = string
82+
EE extends string = string,
83+
I extends ComponentInjectOptions = {},
84+
II extends string = string
7885
>(
7986
options: ComponentOptionsWithArrayProps<
8087
PropNames,
@@ -85,7 +92,9 @@ export function defineCustomElement<
8592
Mixin,
8693
Extends,
8794
E,
88-
EE
95+
EE,
96+
I,
97+
II
8998
> & { styles?: string[] }
9099
): VueElementConstructor<{ [K in PropNames]: any }>
91100

@@ -99,7 +108,9 @@ export function defineCustomElement<
99108
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
100109
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
101110
E extends EmitsOptions = Record<string, any>,
102-
EE extends string = string
111+
EE extends string = string,
112+
I extends ComponentInjectOptions = {},
113+
II extends string = string
103114
>(
104115
options: ComponentOptionsWithObjectProps<
105116
PropsOptions,
@@ -110,7 +121,9 @@ export function defineCustomElement<
110121
Mixin,
111122
Extends,
112123
E,
113-
EE
124+
EE,
125+
I,
126+
II
114127
> & { styles?: string[] }
115128
): VueElementConstructor<ExtractPropTypes<PropsOptions>>
116129

0 commit comments

Comments
 (0)