Skip to content

Commit

Permalink
chore: improve code according to review advice
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyxu1102 committed Sep 2, 2023
1 parent f220643 commit f002f67
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 33 deletions.
15 changes: 14 additions & 1 deletion packages/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ describe('define attrs', () => {
expectType<JSX.Element>(<MyComp />)
})

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

test('define no attrs w/ functional component', () => {
const MyComp = defineComponent((props: { foo: string }, ctx) => {
expectType<unknown>(ctx.attrs.bar)
return () => (
// return a render function (both JSX and h() works)
<div>{props.foo}</div>
)
})
expectType<JSX.Element>(<MyComp foo={'1'} />)
// @ts-expect-error
expectType<JSX.Element>(<MyComp foo="1" bar={1} />)
})

test('default attrs like class, style', () => {
const MyComp = defineComponent({
props: {
Expand Down
17 changes: 8 additions & 9 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ import {
ComponentObjectPropsOptions
} from './componentProps'
import { EmitsOptions, EmitsToProps } from './componentEmits'
import { extend, isFunction } from '@vue/shared'
import { IsSameType, extend, isFunction } from '@vue/shared'
import { VNodeProps } from './vnode'
import {
CreateComponentPublicInstance,
ComponentPublicInstanceConstructor,
IsSameType
ComponentPublicInstanceConstructor
} from './componentPublicInstance'
import { SlotsType } from './componentSlots'

Expand Down Expand Up @@ -58,7 +57,7 @@ export type DefineComponent<
Props = ResolveProps<PropsOrPropOptions, E>,
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined
Attrs extends AttrsType = Record<string, unknown>
> = ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
Expand Down Expand Up @@ -108,8 +107,8 @@ export function defineComponent<
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined,
PropsAttrs = IsSameType<undefined, Attrs> extends true
Attrs extends AttrsType = Record<string, unknown>,
PropsAttrs = IsSameType<string, keyof Attrs> extends true
? {}
: UnwrapAttrsType<NonNullable<Attrs>>
>(
Expand Down Expand Up @@ -155,7 +154,7 @@ export function defineComponent<
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined,
Attrs extends AttrsType = Record<string, unknown>,
I extends ComponentInjectOptions = {},
II extends string = string
>(
Expand Down Expand Up @@ -205,7 +204,7 @@ export function defineComponent<
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined,
Attrs extends AttrsType = Record<string, unknown>,
I extends ComponentInjectOptions = {},
II extends string = string,
Props = Readonly<{ [key in PropNames]?: any }>
Expand Down Expand Up @@ -259,7 +258,7 @@ export function defineComponent<
S extends SlotsType = {},
I extends ComponentInjectOptions = {},
II extends string = string,
Attrs extends AttrsType | undefined = undefined
Attrs extends AttrsType = Record<string, unknown>
>(
options: ComponentOptionsWithObjectProps<
PropsOptions,
Expand Down
10 changes: 5 additions & 5 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import {
exposeSetupStateOnRenderContext,
ComponentPublicInstanceConstructor,
publicPropertiesMap,
RuntimeCompiledPublicInstanceProxyHandlers,
IsSameType
RuntimeCompiledPublicInstanceProxyHandlers
} from './componentPublicInstance'
import {
ComponentPropsOptions,
Expand Down Expand Up @@ -67,7 +66,8 @@ import {
ShapeFlags,
extend,
getGlobalThis,
IfAny
IfAny,
IsSameType
} from '@vue/shared'
import { SuspenseBoundary } from './components/Suspense'
import { CompilerOptions } from '@vue/compiler-core'
Expand Down Expand Up @@ -188,10 +188,10 @@ type LifecycleHook<TFn = Function> = TFn[] | null
export type SetupContext<
E = EmitsOptions,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined
Attrs extends AttrsType = Record<string, unknown>
> = E extends any
? {
attrs: IsSameType<Attrs, undefined> extends true
attrs: IsSameType<keyof Attrs, string> extends true
? Data
: UnwrapAttrsType<NonNullable<Attrs>>
slots: UnwrapSlotsType<S>
Expand Down
8 changes: 4 additions & 4 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export interface ComponentOptionsBase<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined
Attrs extends AttrsType = Record<string, unknown>
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, I, II>,
ComponentInternalOptions,
ComponentCustomOptions {
Expand Down Expand Up @@ -226,7 +226,7 @@ export type ComponentOptionsWithoutProps<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined,
Attrs extends AttrsType = Record<string, unknown>,
PE = Props & EmitsToProps<E>
> = ComponentOptionsBase<
PE,
Expand Down Expand Up @@ -277,7 +277,7 @@ export type ComponentOptionsWithArrayProps<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined,
Attrs extends AttrsType = Record<string, unknown>,
Props = Prettify<Readonly<{ [key in PropNames]?: any } & EmitsToProps<E>>>
> = ComponentOptionsBase<
Props,
Expand Down Expand Up @@ -328,7 +328,7 @@ export type ComponentOptionsWithObjectProps<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined,
Attrs extends AttrsType = Record<string, unknown>,
Props = Prettify<Readonly<ExtractPropTypes<PropsOptions> & EmitsToProps<E>>>,
Defaults = ExtractDefaultPropTypes<PropsOptions>
> = ComponentOptionsBase<
Expand Down
17 changes: 6 additions & 11 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
isString,
isFunction,
UnionToIntersection,
Prettify
Prettify,
IsSameType
} from '@vue/shared'
import {
toRaw,
Expand Down Expand Up @@ -152,7 +153,7 @@ export type CreateComponentPublicInstance<
MakeDefaultsOptional extends boolean = false,
I extends ComponentInjectOptions = {},
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined,
Attrs extends AttrsType = Record<string, unknown>,
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
Expand Down Expand Up @@ -194,12 +195,6 @@ export type CreateComponentPublicInstance<
Attrs
>

export type IsSameType<T, U> = T extends U
? U extends T
? true
: false
: false

// public properties exposed on the proxy, which is used as the render context
// in templates (as `this` in the render option)
export type ComponentPublicInstance<
Expand All @@ -215,8 +210,8 @@ export type ComponentPublicInstance<
Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>,
I extends ComponentInjectOptions = {},
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined,
PropsAttrs = IsSameType<Attrs, undefined> extends true
Attrs extends AttrsType = Record<string, unknown>,
PropsAttrs = IsSameType<keyof Attrs, string> extends true
? {}
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)>
> = {
Expand All @@ -227,7 +222,7 @@ export type ComponentPublicInstance<
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults> & PropsAttrs
: P & PublicProps & PropsAttrs
>
$attrs: IsSameType<Attrs, undefined> extends true
$attrs: IsSameType<keyof Attrs, string> extends true
? Data
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)> &
AllowedComponentProps
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function defineCustomElement<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined
Attrs extends AttrsType = Record<string, unknown>
>(
options: ComponentOptionsWithoutProps<
Props,
Expand Down Expand Up @@ -89,7 +89,7 @@ export function defineCustomElement<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined
Attrs extends AttrsType = Record<string, unknown>
>(
options: ComponentOptionsWithArrayProps<
PropNames,
Expand Down Expand Up @@ -122,7 +122,7 @@ export function defineCustomElement<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType | undefined = undefined
Attrs extends AttrsType = Record<string, unknown>
>(
options: ComponentOptionsWithObjectProps<
PropsOptions,
Expand Down
6 changes: 6 additions & 0 deletions packages/shared/src/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ export type Awaited<T> = T extends null | undefined
? Awaited<V> // recursively unwrap the value
: never // the argument to `then` was not callable
: T // non-object or non-thenable

export type IsSameType<T, U> = T extends U
? U extends T
? true
: false
: false

0 comments on commit f002f67

Please sign in to comment.