Skip to content

Commit f002f67

Browse files
committed
chore: improve code according to review advice
1 parent f220643 commit f002f67

File tree

7 files changed

+46
-33
lines changed

7 files changed

+46
-33
lines changed

packages/dts-test/defineComponent.test-d.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ describe('define attrs', () => {
12991299
expectType<JSX.Element>(<MyComp />)
13001300
})
13011301

1302-
test('define attrs w/ no attrs', () => {
1302+
test('define no attrs w/ object props', () => {
13031303
const MyComp = defineComponent({
13041304
props: {
13051305
foo: String
@@ -1312,6 +1312,19 @@ describe('define attrs', () => {
13121312
expectType<JSX.Element>(<MyComp foo="1" bar={1} />)
13131313
})
13141314

1315+
test('define no attrs w/ functional component', () => {
1316+
const MyComp = defineComponent((props: { foo: string }, ctx) => {
1317+
expectType<unknown>(ctx.attrs.bar)
1318+
return () => (
1319+
// return a render function (both JSX and h() works)
1320+
<div>{props.foo}</div>
1321+
)
1322+
})
1323+
expectType<JSX.Element>(<MyComp foo={'1'} />)
1324+
// @ts-expect-error
1325+
expectType<JSX.Element>(<MyComp foo="1" bar={1} />)
1326+
})
1327+
13151328
test('default attrs like class, style', () => {
13161329
const MyComp = defineComponent({
13171330
props: {

packages/runtime-core/src/apiDefineComponent.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ import {
2424
ComponentObjectPropsOptions
2525
} from './componentProps'
2626
import { EmitsOptions, EmitsToProps } from './componentEmits'
27-
import { extend, isFunction } from '@vue/shared'
27+
import { IsSameType, extend, isFunction } from '@vue/shared'
2828
import { VNodeProps } from './vnode'
2929
import {
3030
CreateComponentPublicInstance,
31-
ComponentPublicInstanceConstructor,
32-
IsSameType
31+
ComponentPublicInstanceConstructor
3332
} from './componentPublicInstance'
3433
import { SlotsType } from './componentSlots'
3534

@@ -58,7 +57,7 @@ export type DefineComponent<
5857
Props = ResolveProps<PropsOrPropOptions, E>,
5958
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>,
6059
S extends SlotsType = {},
61-
Attrs extends AttrsType | undefined = undefined
60+
Attrs extends AttrsType = Record<string, unknown>
6261
> = ComponentPublicInstanceConstructor<
6362
CreateComponentPublicInstance<
6463
Props,
@@ -108,8 +107,8 @@ export function defineComponent<
108107
E extends EmitsOptions = {},
109108
EE extends string = string,
110109
S extends SlotsType = {},
111-
Attrs extends AttrsType | undefined = undefined,
112-
PropsAttrs = IsSameType<undefined, Attrs> extends true
110+
Attrs extends AttrsType = Record<string, unknown>,
111+
PropsAttrs = IsSameType<string, keyof Attrs> extends true
113112
? {}
114113
: UnwrapAttrsType<NonNullable<Attrs>>
115114
>(
@@ -155,7 +154,7 @@ export function defineComponent<
155154
E extends EmitsOptions = {},
156155
EE extends string = string,
157156
S extends SlotsType = {},
158-
Attrs extends AttrsType | undefined = undefined,
157+
Attrs extends AttrsType = Record<string, unknown>,
159158
I extends ComponentInjectOptions = {},
160159
II extends string = string
161160
>(
@@ -205,7 +204,7 @@ export function defineComponent<
205204
E extends EmitsOptions = {},
206205
EE extends string = string,
207206
S extends SlotsType = {},
208-
Attrs extends AttrsType | undefined = undefined,
207+
Attrs extends AttrsType = Record<string, unknown>,
209208
I extends ComponentInjectOptions = {},
210209
II extends string = string,
211210
Props = Readonly<{ [key in PropNames]?: any }>
@@ -259,7 +258,7 @@ export function defineComponent<
259258
S extends SlotsType = {},
260259
I extends ComponentInjectOptions = {},
261260
II extends string = string,
262-
Attrs extends AttrsType | undefined = undefined
261+
Attrs extends AttrsType = Record<string, unknown>
263262
>(
264263
options: ComponentOptionsWithObjectProps<
265264
PropsOptions,

packages/runtime-core/src/component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import {
1919
exposeSetupStateOnRenderContext,
2020
ComponentPublicInstanceConstructor,
2121
publicPropertiesMap,
22-
RuntimeCompiledPublicInstanceProxyHandlers,
23-
IsSameType
22+
RuntimeCompiledPublicInstanceProxyHandlers
2423
} from './componentPublicInstance'
2524
import {
2625
ComponentPropsOptions,
@@ -67,7 +66,8 @@ import {
6766
ShapeFlags,
6867
extend,
6968
getGlobalThis,
70-
IfAny
69+
IfAny,
70+
IsSameType
7171
} from '@vue/shared'
7272
import { SuspenseBoundary } from './components/Suspense'
7373
import { CompilerOptions } from '@vue/compiler-core'
@@ -188,10 +188,10 @@ type LifecycleHook<TFn = Function> = TFn[] | null
188188
export type SetupContext<
189189
E = EmitsOptions,
190190
S extends SlotsType = {},
191-
Attrs extends AttrsType | undefined = undefined
191+
Attrs extends AttrsType = Record<string, unknown>
192192
> = E extends any
193193
? {
194-
attrs: IsSameType<Attrs, undefined> extends true
194+
attrs: IsSameType<keyof Attrs, string> extends true
195195
? Data
196196
: UnwrapAttrsType<NonNullable<Attrs>>
197197
slots: UnwrapSlotsType<S>

packages/runtime-core/src/componentOptions.ts

Lines changed: 4 additions & 4 deletions
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 | undefined = undefined
115+
Attrs extends AttrsType = Record<string, unknown>
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 | undefined = undefined,
229+
Attrs extends AttrsType = Record<string, unknown>,
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 | undefined = undefined,
280+
Attrs extends AttrsType = Record<string, unknown>,
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 | undefined = undefined,
331+
Attrs extends AttrsType = Record<string, unknown>,
332332
Props = Prettify<Readonly<ExtractPropTypes<PropsOptions> & EmitsToProps<E>>>,
333333
Defaults = ExtractDefaultPropTypes<PropsOptions>
334334
> = ComponentOptionsBase<

packages/runtime-core/src/componentPublicInstance.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
isString,
1717
isFunction,
1818
UnionToIntersection,
19-
Prettify
19+
Prettify,
20+
IsSameType
2021
} from '@vue/shared'
2122
import {
2223
toRaw,
@@ -152,7 +153,7 @@ export type CreateComponentPublicInstance<
152153
MakeDefaultsOptional extends boolean = false,
153154
I extends ComponentInjectOptions = {},
154155
S extends SlotsType = {},
155-
Attrs extends AttrsType | undefined = undefined,
156+
Attrs extends AttrsType = Record<string, unknown>,
156157
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
157158
PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
158159
PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
@@ -194,12 +195,6 @@ export type CreateComponentPublicInstance<
194195
Attrs
195196
>
196197

197-
export type IsSameType<T, U> = T extends U
198-
? U extends T
199-
? true
200-
: false
201-
: false
202-
203198
// public properties exposed on the proxy, which is used as the render context
204199
// in templates (as `this` in the render option)
205200
export type ComponentPublicInstance<
@@ -215,8 +210,8 @@ export type ComponentPublicInstance<
215210
Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>,
216211
I extends ComponentInjectOptions = {},
217212
S extends SlotsType = {},
218-
Attrs extends AttrsType | undefined = undefined,
219-
PropsAttrs = IsSameType<Attrs, undefined> extends true
213+
Attrs extends AttrsType = Record<string, unknown>,
214+
PropsAttrs = IsSameType<keyof Attrs, string> extends true
220215
? {}
221216
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)>
222217
> = {
@@ -227,7 +222,7 @@ export type ComponentPublicInstance<
227222
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults> & PropsAttrs
228223
: P & PublicProps & PropsAttrs
229224
>
230-
$attrs: IsSameType<Attrs, undefined> extends true
225+
$attrs: IsSameType<keyof Attrs, string> extends true
231226
? Data
232227
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)> &
233228
AllowedComponentProps

packages/runtime-dom/src/apiCustomElement.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function defineCustomElement<
5656
I extends ComponentInjectOptions = {},
5757
II extends string = string,
5858
S extends SlotsType = {},
59-
Attrs extends AttrsType | undefined = undefined
59+
Attrs extends AttrsType = Record<string, unknown>
6060
>(
6161
options: ComponentOptionsWithoutProps<
6262
Props,
@@ -89,7 +89,7 @@ export function defineCustomElement<
8989
I extends ComponentInjectOptions = {},
9090
II extends string = string,
9191
S extends SlotsType = {},
92-
Attrs extends AttrsType | undefined = undefined
92+
Attrs extends AttrsType = Record<string, unknown>
9393
>(
9494
options: ComponentOptionsWithArrayProps<
9595
PropNames,
@@ -122,7 +122,7 @@ export function defineCustomElement<
122122
I extends ComponentInjectOptions = {},
123123
II extends string = string,
124124
S extends SlotsType = {},
125-
Attrs extends AttrsType | undefined = undefined
125+
Attrs extends AttrsType = Record<string, unknown>
126126
>(
127127
options: ComponentOptionsWithObjectProps<
128128
PropsOptions,

packages/shared/src/typeUtils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ export type Awaited<T> = T extends null | undefined
2121
? Awaited<V> // recursively unwrap the value
2222
: never // the argument to `then` was not callable
2323
: T // non-object or non-thenable
24+
25+
export type IsSameType<T, U> = T extends U
26+
? U extends T
27+
? true
28+
: false
29+
: false

0 commit comments

Comments
 (0)