Skip to content

Commit 964f9f3

Browse files
authored
fix(type): vue constructor should not require props with default values (#567)
Co-authored-by: vail <[email protected]>
1 parent 555f20a commit 964f9f3

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/component/componentProps.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@ export type ExtractPropTypes<O> = O extends object
6363
? { [K in RequiredKeys<O>]: InferPropType<O[K]> } &
6464
{ [K in OptionalKeys<O>]?: InferPropType<O[K]> }
6565
: { [K in string]: any }
66+
67+
type DefaultKeys<T> = {
68+
[K in keyof T]: T[K] extends { default: any } ? K : never
69+
}[keyof T]
70+
71+
// extract props which defined with default from prop options
72+
export type ExtractDefaultPropTypes<O> = O extends object
73+
? { [K in DefaultKeys<O>]: InferPropType<O[K]> }
74+
: {}

src/component/componentProxy.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExtractPropTypes } from './componentProps'
1+
import { ExtractDefaultPropTypes, ExtractPropTypes } from './componentProps'
22
import { ShallowUnwrapRef } from '..'
33
import { Data } from './common'
44

@@ -22,10 +22,16 @@ export type ComponentRenderProxy<
2222
D = {}, // return from data()
2323
C extends ComputedOptions = {},
2424
M extends MethodOptions = {},
25-
PublicProps = P
25+
PublicProps = P,
26+
Defaults = {},
27+
MakeDefaultsOptional extends boolean = false
2628
> = {
2729
$data: D
28-
$props: Readonly<P & PublicProps>
30+
$props: Readonly<
31+
MakeDefaultsOptional extends true
32+
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
33+
: P & PublicProps
34+
>
2935
$attrs: Data
3036
} & Readonly<P> &
3137
ShallowUnwrapRef<B> &
@@ -39,7 +45,12 @@ type VueConstructorProxy<PropsOptions, RawBindings> = VueConstructor & {
3945
new (...args: any[]): ComponentRenderProxy<
4046
ExtractPropTypes<PropsOptions>,
4147
ShallowUnwrapRef<RawBindings>,
42-
ExtractPropTypes<PropsOptions>
48+
ExtractPropTypes<PropsOptions>,
49+
{},
50+
{},
51+
ExtractPropTypes<PropsOptions>,
52+
ExtractDefaultPropTypes<PropsOptions>,
53+
true
4354
>
4455
}
4556

0 commit comments

Comments
 (0)