Skip to content

Commit

Permalink
feat(dx): improve readability of displayed types for props
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 24, 2023
1 parent 58e5c51 commit 4c9bfd2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
20 changes: 11 additions & 9 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, isPromise, isFunction } from '@vue/shared'
import { isArray, isPromise, isFunction, Prettify } from '@vue/shared'
import {
getCurrentInstance,
setCurrentInstance,
Expand Down Expand Up @@ -55,18 +55,20 @@ const warnRuntimeUsage = (method: string) =>
// overload 1: runtime props w/ array
export function defineProps<PropNames extends string = string>(
props: PropNames[]
): Readonly<{ [key in PropNames]?: any }>
): Prettify<Readonly<{ [key in PropNames]?: any }>>
// overload 2: runtime props w/ object
export function defineProps<
PP extends ComponentObjectPropsOptions = ComponentObjectPropsOptions
>(props: PP): Readonly<ExtractPropTypes<PP>>
>(props: PP): Prettify<Readonly<ExtractPropTypes<PP>>>
// overload 3: typed-based declaration
export function defineProps<TypeProps>(): Readonly<
Omit<TypeProps, BooleanKey<TypeProps>> & {
[K in keyof Pick<TypeProps, BooleanKey<TypeProps>>]-?: NotUndefined<
TypeProps[K]
>
}
export function defineProps<TypeProps>(): Prettify<
Readonly<
Omit<TypeProps, BooleanKey<TypeProps>> & {
[K in keyof Pick<TypeProps, BooleanKey<TypeProps>>]-?: NotUndefined<
TypeProps[K]
>
}
>
>
// implementation
export function defineProps() {
Expand Down
15 changes: 8 additions & 7 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
isArray,
NOOP,
isPromise,
LooseRequired
LooseRequired,
Prettify
} from '@vue/shared'
import { isRef, Ref } from '@vue/reactivity'
import { computed } from './apiComputed'
Expand Down Expand Up @@ -112,14 +113,14 @@ export interface ComponentOptionsBase<
ComponentCustomOptions {
setup?: (
this: void,
props: Readonly<
LooseRequired<
Props &
props: LooseRequired<
Props &
Prettify<
UnwrapMixinsType<
IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
'P'
>
>
>
>,
ctx: SetupContext<E>
) => Promise<RawBindings> | RawBindings | RenderFunction | void
Expand Down Expand Up @@ -262,7 +263,7 @@ export type ComponentOptionsWithArrayProps<
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string,
Props = Readonly<{ [key in PropNames]?: any }> & EmitsToProps<E>
Props = Prettify<Readonly<{ [key in PropNames]?: any } & EmitsToProps<E>>>
> = ComponentOptionsBase<
Props,
RawBindings,
Expand Down Expand Up @@ -307,7 +308,7 @@ export type ComponentOptionsWithObjectProps<
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string,
Props = Readonly<ExtractPropTypes<PropsOptions>> & EmitsToProps<E>,
Props = Prettify<Readonly<ExtractPropTypes<PropsOptions> & EmitsToProps<E>>>,
Defaults = ExtractDefaultPropTypes<PropsOptions>
> = ComponentOptionsBase<
Props,
Expand Down
11 changes: 7 additions & 4 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
extend,
isString,
isFunction,
UnionToIntersection
UnionToIntersection,
Prettify
} from '@vue/shared'
import {
toRaw,
Expand Down Expand Up @@ -185,9 +186,11 @@ export type ComponentPublicInstance<
> = {
$: ComponentInternalInstance
$data: D
$props: MakeDefaultsOptional extends true
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
: P & PublicProps
$props: Prettify<
MakeDefaultsOptional extends true
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
: P & PublicProps
>
$attrs: Data
$refs: Data
$slots: Slots
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/typeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type Prettify<T> = { [K in keyof T]: T[K] } & {}

export type UnionToIntersection<U> = (
U extends any ? (k: U) => void : never
) extends (k: infer I) => void
Expand Down

0 comments on commit 4c9bfd2

Please sign in to comment.