Skip to content

Commit f977e10

Browse files
committed
chore: improve code readability
1 parent 75394bf commit f977e10

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

packages/runtime-core/src/apiDefineComponent.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
import {
1616
SetupContext,
1717
AllowedComponentProps,
18-
ComponentCustomProps
18+
ComponentCustomProps,
19+
HasDefineAttrs
1920
} from './component'
2021
import {
2122
ExtractPropTypes,
@@ -24,7 +25,7 @@ import {
2425
ComponentObjectPropsOptions
2526
} from './componentProps'
2627
import { EmitsOptions, EmitsToProps } from './componentEmits'
27-
import { IsSameType, extend, isFunction } from '@vue/shared'
28+
import { extend, isFunction } from '@vue/shared'
2829
import { VNodeProps } from './vnode'
2930
import {
3031
CreateComponentPublicInstance,
@@ -108,7 +109,7 @@ export function defineComponent<
108109
EE extends string = string,
109110
S extends SlotsType = {},
110111
Attrs extends AttrsType = Record<string, unknown>,
111-
PropsAttrs = IsSameType<string, keyof Attrs> extends true
112+
PropsAttrs = HasDefineAttrs<Attrs> extends true
112113
? {}
113114
: UnwrapAttrsType<NonNullable<Attrs>>
114115
>(

packages/runtime-core/src/component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import {
6767
extend,
6868
getGlobalThis,
6969
IfAny,
70-
IsSameType
70+
Equal
7171
} from '@vue/shared'
7272
import { SuspenseBoundary } from './components/Suspense'
7373
import { CompilerOptions } from '@vue/compiler-core'
@@ -84,7 +84,7 @@ import { SchedulerJob } from './scheduler'
8484
import { LifecycleHooks } from './enums'
8585

8686
export type Data = Record<string, unknown>
87-
87+
export type HasDefineAttrs<T> = Equal<keyof T, string>
8888
/**
8989
* For extending allowed non-declared props on components in TSX
9090
*/
@@ -191,7 +191,7 @@ export type SetupContext<
191191
Attrs extends AttrsType = Record<string, unknown>
192192
> = E extends any
193193
? {
194-
attrs: IsSameType<keyof Attrs, string> extends true
194+
attrs: HasDefineAttrs<Attrs> extends true
195195
? Data
196196
: UnwrapAttrsType<NonNullable<Attrs>>
197197
slots: UnwrapSlotsType<S>

packages/runtime-core/src/componentPublicInstance.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AllowedComponentProps,
33
ComponentInternalInstance,
44
Data,
5+
HasDefineAttrs,
56
getExposeProxy,
67
isStatefulComponent
78
} from './component'
@@ -16,8 +17,7 @@ import {
1617
isString,
1718
isFunction,
1819
UnionToIntersection,
19-
Prettify,
20-
IsSameType
20+
Prettify
2121
} from '@vue/shared'
2222
import {
2323
toRaw,
@@ -211,9 +211,9 @@ export type ComponentPublicInstance<
211211
I extends ComponentInjectOptions = {},
212212
S extends SlotsType = {},
213213
Attrs extends AttrsType = Record<string, unknown>,
214-
PropsAttrs = IsSameType<keyof Attrs, string> extends true
214+
PropsAttrs = HasDefineAttrs<Attrs> extends true
215215
? {}
216-
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)>
216+
: Omit<UnwrapAttrsType<Attrs>, keyof (P & PublicProps)>
217217
> = {
218218
$: ComponentInternalInstance
219219
$data: D
@@ -222,9 +222,9 @@ export type ComponentPublicInstance<
222222
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults> & PropsAttrs
223223
: P & PublicProps & PropsAttrs
224224
>
225-
$attrs: IsSameType<keyof Attrs, string> extends true
225+
$attrs: HasDefineAttrs<Attrs> extends true
226226
? Data
227-
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)> &
227+
: Omit<UnwrapAttrsType<Attrs>, keyof (P & PublicProps)> &
228228
AllowedComponentProps
229229
$refs: Data
230230
$slots: UnwrapSlotsType<S>

packages/shared/src/typeUtils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export type Awaited<T> = T extends null | undefined
2222
: never // the argument to `then` was not callable
2323
: T // non-object or non-thenable
2424

25-
export type IsSameType<T, U> = T extends U
26-
? U extends T
27-
? true
28-
: false
29-
: false
25+
// Conditional returns can enforce identical types.
26+
// 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;

0 commit comments

Comments
 (0)