Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit df90c7c

Browse files
author
rudyxu
committedSep 1, 2023
chore: improve code
1 parent b8d6793 commit df90c7c

File tree

5 files changed

+35
-34
lines changed

5 files changed

+35
-34
lines changed
 

‎packages/runtime-core/src/apiDefineComponent.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import {
1515
import {
1616
SetupContext,
1717
AllowedComponentProps,
18-
ComponentCustomProps,
19-
Data
18+
ComponentCustomProps
2019
} from './component'
2120
import {
2221
ExtractPropTypes,
@@ -59,7 +58,7 @@ export type DefineComponent<
5958
Props = ResolveProps<PropsOrPropOptions, E>,
6059
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>,
6160
S extends SlotsType = {},
62-
Attrs extends AttrsType = {}
61+
Attrs extends AttrsType | undefined = undefined
6362
> = ComponentPublicInstanceConstructor<
6463
CreateComponentPublicInstance<
6564
Props,
@@ -109,10 +108,10 @@ export function defineComponent<
109108
E extends EmitsOptions = {},
110109
EE extends string = string,
111110
S extends SlotsType = {},
112-
Attrs extends AttrsType = {},
113-
PropsAttrs = IsSameType<Data, UnwrapAttrsType<Attrs>> extends true
111+
Attrs extends AttrsType | undefined = undefined,
112+
PropsAttrs = IsSameType<undefined, Attrs> extends true
114113
? {}
115-
: UnwrapAttrsType<Attrs>
114+
: UnwrapAttrsType<NonNullable<Attrs>>
116115
>(
117116
setup: (
118117
props: Props,
@@ -156,7 +155,7 @@ export function defineComponent<
156155
E extends EmitsOptions = {},
157156
EE extends string = string,
158157
S extends SlotsType = {},
159-
Attrs extends AttrsType = {},
158+
Attrs extends AttrsType | undefined = undefined,
160159
I extends ComponentInjectOptions = {},
161160
II extends string = string
162161
>(
@@ -206,7 +205,7 @@ export function defineComponent<
206205
E extends EmitsOptions = {},
207206
EE extends string = string,
208207
S extends SlotsType = {},
209-
Attrs extends AttrsType = {},
208+
Attrs extends AttrsType | undefined = undefined,
210209
I extends ComponentInjectOptions = {},
211210
II extends string = string,
212211
Props = Readonly<{ [key in PropNames]?: any }>
@@ -260,7 +259,7 @@ export function defineComponent<
260259
S extends SlotsType = {},
261260
I extends ComponentInjectOptions = {},
262261
II extends string = string,
263-
Attrs extends AttrsType = {}
262+
Attrs extends AttrsType | undefined = undefined
264263
>(
265264
options: ComponentOptionsWithObjectProps<
266265
PropsOptions,

‎packages/runtime-core/src/component.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
exposeSetupStateOnRenderContext,
2020
ComponentPublicInstanceConstructor,
2121
publicPropertiesMap,
22-
RuntimeCompiledPublicInstanceProxyHandlers
22+
RuntimeCompiledPublicInstanceProxyHandlers,
23+
IsSameType
2324
} from './componentPublicInstance'
2425
import {
2526
ComponentPropsOptions,
@@ -187,10 +188,12 @@ type LifecycleHook<TFn = Function> = TFn[] | null
187188
export type SetupContext<
188189
E = EmitsOptions,
189190
S extends SlotsType = {},
190-
Attrs extends AttrsType = {}
191+
Attrs extends AttrsType | undefined = undefined
191192
> = E extends any
192193
? {
193-
attrs: Partial<UnwrapAttrsType<Attrs>>
194+
attrs: IsSameType<Attrs, undefined> extends true
195+
? Data
196+
: UnwrapAttrsType<NonNullable<Attrs>>
194197
slots: UnwrapSlotsType<S>
195198
emit: EmitFn<E>
196199
expose: (exposed?: Record<string, any>) => void

‎packages/runtime-core/src/componentOptions.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export interface ComponentOptionsBase<
112112
I extends ComponentInjectOptions = {},
113113
II extends string = string,
114114
S extends SlotsType = {},
115-
Attrs extends AttrsType = {}
115+
Attrs extends AttrsType | undefined = undefined
116116
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, I, II>,
117117
ComponentInternalOptions,
118118
ComponentCustomOptions {
@@ -226,7 +226,7 @@ export type ComponentOptionsWithoutProps<
226226
I extends ComponentInjectOptions = {},
227227
II extends string = string,
228228
S extends SlotsType = {},
229-
Attrs extends AttrsType = {},
229+
Attrs extends AttrsType | undefined = undefined,
230230
PE = Props & EmitsToProps<E>
231231
> = ComponentOptionsBase<
232232
PE,
@@ -277,7 +277,7 @@ export type ComponentOptionsWithArrayProps<
277277
I extends ComponentInjectOptions = {},
278278
II extends string = string,
279279
S extends SlotsType = {},
280-
Attrs extends AttrsType = {},
280+
Attrs extends AttrsType | undefined = undefined,
281281
Props = Prettify<Readonly<{ [key in PropNames]?: any } & EmitsToProps<E>>>
282282
> = ComponentOptionsBase<
283283
Props,
@@ -328,7 +328,7 @@ export type ComponentOptionsWithObjectProps<
328328
I extends ComponentInjectOptions = {},
329329
II extends string = string,
330330
S extends SlotsType = {},
331-
Attrs extends AttrsType = {},
331+
Attrs extends AttrsType | undefined = undefined,
332332
Props = Prettify<Readonly<ExtractPropTypes<PropsOptions> & EmitsToProps<E>>>,
333333
Defaults = ExtractDefaultPropTypes<PropsOptions>
334334
> = ComponentOptionsBase<
@@ -421,17 +421,17 @@ declare const AttrSymbol: unique symbol
421421
export type AttrsType<T extends Record<string, any> = Record<string, any>> = {
422422
[AttrSymbol]?: T
423423
}
424+
424425
export type UnwrapAttrsType<
425-
S extends AttrsType,
426-
T = NonNullable<S[typeof AttrSymbol]>
427-
> = [keyof S] extends [never]
426+
Attrs extends AttrsType,
427+
T = NonNullable<Attrs[typeof AttrSymbol]>
428+
> = [keyof Attrs] extends [never]
428429
? Data
429430
: Readonly<
430431
Prettify<{
431432
[K in keyof T]: T[K]
432433
}>
433434
>
434-
435435
export type ComputedOptions = Record<
436436
string,
437437
ComputedGetter<any> | WritableComputedOptions<any>

‎packages/runtime-core/src/componentPublicInstance.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export type CreateComponentPublicInstance<
152152
MakeDefaultsOptional extends boolean = false,
153153
I extends ComponentInjectOptions = {},
154154
S extends SlotsType = {},
155-
Attrs extends AttrsType = {},
155+
Attrs extends AttrsType | undefined = undefined,
156156
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
157157
PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
158158
PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
@@ -215,10 +215,10 @@ export type ComponentPublicInstance<
215215
Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>,
216216
I extends ComponentInjectOptions = {},
217217
S extends SlotsType = {},
218-
Attrs extends AttrsType = {},
219-
PropsAttrs = IsSameType<UnwrapAttrsType<Attrs>, Data> extends true
218+
Attrs extends AttrsType | undefined = undefined,
219+
PropsAttrs = IsSameType<Attrs, undefined> extends true
220220
? {}
221-
: Omit<UnwrapAttrsType<Attrs>, keyof (P & PublicProps)>
221+
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)>
222222
> = {
223223
$: ComponentInternalInstance
224224
$data: D
@@ -227,8 +227,10 @@ export type ComponentPublicInstance<
227227
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults> & PropsAttrs
228228
: P & PublicProps & PropsAttrs
229229
>
230-
$attrs: Omit<UnwrapAttrsType<Attrs>, keyof (P & PublicProps)> &
231-
AllowedComponentProps
230+
$attrs: IsSameType<Attrs, undefined> extends true
231+
? Data
232+
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)> &
233+
AllowedComponentProps
232234
$refs: Data
233235
$slots: UnwrapSlotsType<S>
234236
$root: ComponentPublicInstance | null

‎packages/runtime-dom/src/apiCustomElement.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ export type VueElementConstructor<P = {}> = {
3535
// so most of the following overloads should be kept in sync w/ defineComponent.
3636

3737
// overload 1: direct setup function
38-
export function defineCustomElement<
39-
Props,
40-
RawBindings = object,
41-
>(
38+
export function defineCustomElement<Props, RawBindings = object>(
4239
setup: (
4340
props: Readonly<Props>,
4441
ctx: SetupContext
45-
) => RawBindings | RenderFunction,
42+
) => RawBindings | RenderFunction
4643
): VueElementConstructor<Props>
4744

4845
// overload 2: object format with no props
@@ -59,7 +56,7 @@ export function defineCustomElement<
5956
I extends ComponentInjectOptions = {},
6057
II extends string = string,
6158
S extends SlotsType = {},
62-
Attrs extends AttrsType = {}
59+
Attrs extends AttrsType | undefined = undefined
6360
>(
6461
options: ComponentOptionsWithoutProps<
6562
Props,
@@ -92,7 +89,7 @@ export function defineCustomElement<
9289
I extends ComponentInjectOptions = {},
9390
II extends string = string,
9491
S extends SlotsType = {},
95-
Attrs extends AttrsType = {}
92+
Attrs extends AttrsType | undefined = undefined
9693
>(
9794
options: ComponentOptionsWithArrayProps<
9895
PropNames,
@@ -125,7 +122,7 @@ export function defineCustomElement<
125122
I extends ComponentInjectOptions = {},
126123
II extends string = string,
127124
S extends SlotsType = {},
128-
Attrs extends AttrsType = {}
125+
Attrs extends AttrsType | undefined = undefined
129126
>(
130127
options: ComponentOptionsWithObjectProps<
131128
PropsOptions,

0 commit comments

Comments
 (0)
Please sign in to comment.