Skip to content

Commit

Permalink
fix(type): vue constructor should not require props with default valu…
Browse files Browse the repository at this point in the history
…es (#567)

Co-authored-by: vail <ysywix@gmail.com>
  • Loading branch information
vaiil and vaiil committed Oct 21, 2020
1 parent 555f20a commit 964f9f3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/component/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ export type ExtractPropTypes<O> = O extends object
? { [K in RequiredKeys<O>]: InferPropType<O[K]> } &
{ [K in OptionalKeys<O>]?: InferPropType<O[K]> }
: { [K in string]: any }

type DefaultKeys<T> = {
[K in keyof T]: T[K] extends { default: any } ? K : never
}[keyof T]

// extract props which defined with default from prop options
export type ExtractDefaultPropTypes<O> = O extends object
? { [K in DefaultKeys<O>]: InferPropType<O[K]> }
: {}
19 changes: 15 additions & 4 deletions src/component/componentProxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtractPropTypes } from './componentProps'
import { ExtractDefaultPropTypes, ExtractPropTypes } from './componentProps'
import { ShallowUnwrapRef } from '..'
import { Data } from './common'

Expand All @@ -22,10 +22,16 @@ export type ComponentRenderProxy<
D = {}, // return from data()
C extends ComputedOptions = {},
M extends MethodOptions = {},
PublicProps = P
PublicProps = P,
Defaults = {},
MakeDefaultsOptional extends boolean = false
> = {
$data: D
$props: Readonly<P & PublicProps>
$props: Readonly<
MakeDefaultsOptional extends true
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
: P & PublicProps
>
$attrs: Data
} & Readonly<P> &
ShallowUnwrapRef<B> &
Expand All @@ -39,7 +45,12 @@ type VueConstructorProxy<PropsOptions, RawBindings> = VueConstructor & {
new (...args: any[]): ComponentRenderProxy<
ExtractPropTypes<PropsOptions>,
ShallowUnwrapRef<RawBindings>,
ExtractPropTypes<PropsOptions>
ExtractPropTypes<PropsOptions>,
{},
{},
ExtractPropTypes<PropsOptions>,
ExtractDefaultPropTypes<PropsOptions>,
true
>
}

Expand Down

0 comments on commit 964f9f3

Please sign in to comment.