Skip to content

Commit ee626b0

Browse files
committed
Merge branch 'feat/infer_attr' of https://github.com/rudyxu1102/core into feat/infer_attr
2 parents e150401 + 439d5b1 commit ee626b0

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

packages/runtime-core/src/apiDefineComponent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
SetupContext,
1717
AllowedComponentProps,
1818
ComponentCustomProps,
19-
HasDefineAttrs
19+
noAttrsDefine
2020
} from './component'
2121
import {
2222
ExtractPropTypes,
@@ -109,7 +109,7 @@ export function defineComponent<
109109
EE extends string = string,
110110
S extends SlotsType = {},
111111
Attrs extends AttrsType = Record<string, unknown>,
112-
PropsAttrs = HasDefineAttrs<Attrs> extends true
112+
PropsAttrs = noAttrsDefine<Attrs> extends true
113113
? {}
114114
: UnwrapAttrsType<NonNullable<Attrs>>
115115
>(

packages/runtime-core/src/component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ import { SchedulerJob } from './scheduler'
8484
import { LifecycleHooks } from './enums'
8585

8686
export type Data = Record<string, unknown>
87-
export type HasDefineAttrs<T> = Equal<keyof T, string>
87+
// Whether the attrs option is not defined
88+
export type noAttrsDefine<T> = Equal<keyof T, string>
8889
/**
8990
* For extending allowed non-declared props on components in TSX
9091
*/
@@ -191,7 +192,7 @@ export type SetupContext<
191192
Attrs extends AttrsType = Record<string, unknown>
192193
> = E extends any
193194
? {
194-
attrs: HasDefineAttrs<Attrs> extends true
195+
attrs: noAttrsDefine<Attrs> extends true
195196
? Data
196197
: UnwrapAttrsType<NonNullable<Attrs>>
197198
slots: UnwrapSlotsType<S>

packages/runtime-core/src/componentPublicInstance.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
ComponentInternalInstance,
33
Data,
4-
HasDefineAttrs,
4+
noAttrsDefine,
55
getExposeProxy,
66
isStatefulComponent
77
} from './component'
@@ -209,19 +209,20 @@ export type ComponentPublicInstance<
209209
Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>,
210210
I extends ComponentInjectOptions = {},
211211
S extends SlotsType = {},
212-
Attrs extends AttrsType = Record<string, unknown>,
213-
PropsAttrs = HasDefineAttrs<Attrs> extends true
214-
? {}
215-
: Omit<UnwrapAttrsType<Attrs>, keyof (P & PublicProps)>
212+
Attrs extends AttrsType = Record<string, unknown>, // Attrs type extracted from attrs option
213+
// AttrsProps type used for JSX validation of attrs
214+
AttrsProps = noAttrsDefine<Attrs> extends true // if attrs is not defined
215+
? {} // no JSX validation of attrs
216+
: Omit<UnwrapAttrsType<Attrs>, keyof (P & PublicProps)> // exclude props from attrs, for JSX validation
216217
> = {
217218
$: ComponentInternalInstance
218219
$data: D
219220
$props: Prettify<
220221
MakeDefaultsOptional extends true
221-
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults> & PropsAttrs
222-
: P & PublicProps & PropsAttrs
222+
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults> & AttrsProps
223+
: P & PublicProps & AttrsProps
223224
>
224-
$attrs: HasDefineAttrs<Attrs> extends true
225+
$attrs: noAttrsDefine<Attrs> extends true
225226
? Data
226227
: Omit<UnwrapAttrsType<Attrs>, keyof (P & PublicProps)>
227228
$refs: Data

packages/shared/src/typeUtils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export type Awaited<T> = T extends null | undefined
2424

2525
// Conditional returns can enforce identical types.
2626
// See here: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650
27-
// prettier-ignore
28-
export type Equal<Left, Right> =
29-
(<U>() => U extends Left ? 1 : 0) extends (<U>() => U extends Right ? 1 : 0) ? true : false;
27+
export type Equal<Left, Right> = (<U>() => U extends Left ? 1 : 0) extends <
28+
U
29+
>() => U extends Right ? 1 : 0
30+
? true
31+
: false

0 commit comments

Comments
 (0)