@@ -43,7 +43,20 @@ export function defineProps<
43
43
PP extends ComponentObjectPropsOptions = ComponentObjectPropsOptions
44
44
> ( props : PP ) : Readonly < ExtractPropTypes < PP > >
45
45
// overload 3: typed-based declaration
46
- export function defineProps < TypeProps > ( ) : Readonly < TypeProps >
46
+ export function defineProps < TypeProps > ( ) : DefineProps <
47
+ TypeProps ,
48
+ BooleanKey < TypeProps >
49
+ >
50
+
51
+ type DefineProps < T , BKeys extends keyof T > = Readonly < T > & {
52
+ readonly [ K in BKeys ] -?: boolean
53
+ }
54
+
55
+ type BooleanKey < T , K extends keyof T = keyof T > = K extends any
56
+ ? [ T [ K ] ] extends [ boolean | undefined ]
57
+ ? K
58
+ : never
59
+ : never
47
60
48
61
/**
49
62
* Vue `<script setup>` compiler macro for declaring a component's emitted
@@ -96,26 +109,26 @@ export function defineExpose<
96
109
type NotUndefined < T > = T extends undefined ? never : T
97
110
98
111
type InferDefaults < T > = {
99
- [ K in keyof T ] ?: InferDefault < T , NotUndefined < T [ K ] > >
112
+ [ K in keyof T ] ?: InferDefault < T , T [ K ] >
100
113
}
101
114
102
- type InferDefault < P , T > = T extends
103
- | null
104
- | number
105
- | string
106
- | boolean
107
- | symbol
108
- | Function
109
- ? T | ( ( props : P ) => T )
110
- : ( props : P ) => T
115
+ type NativeType = null | number | string | boolean | symbol | Function
111
116
112
- type PropsWithDefaults < Base , Defaults > = Base & {
113
- [ K in keyof Defaults ] : K extends keyof Base
114
- ? Defaults [ K ] extends undefined
115
- ? Base [ K ]
116
- : NotUndefined < Base [ K ] >
117
- : never
118
- }
117
+ type InferDefault < P , T > =
118
+ | ( ( props : P ) => T & { } )
119
+ | ( T extends NativeType ? T : never )
120
+
121
+ type PropsWithDefaults <
122
+ T ,
123
+ Defaults extends InferDefaults < T > ,
124
+ BKeys extends keyof T
125
+ > = Omit < T , keyof Defaults > & {
126
+ [ K in keyof Defaults ] -?: K extends keyof T
127
+ ? Defaults [ K ] extends undefined
128
+ ? T [ K ]
129
+ : NotUndefined < T [ K ] >
130
+ : never
131
+ } & { readonly [ K in BKeys ] -?: boolean }
119
132
120
133
/**
121
134
* Vue `<script setup>` compiler macro for providing props default values when
@@ -135,10 +148,14 @@ type PropsWithDefaults<Base, Defaults> = Base & {
135
148
* This is only usable inside `<script setup>`, is compiled away in the output
136
149
* and should **not** be actually called at runtime.
137
150
*/
138
- export function withDefaults < Props , Defaults extends InferDefaults < Props > > (
139
- props : Props ,
151
+ export function withDefaults <
152
+ T ,
153
+ BKeys extends keyof T ,
154
+ Defaults extends InferDefaults < T >
155
+ > (
156
+ props : DefineProps < T , BKeys > ,
140
157
defaults : Defaults
141
- ) : PropsWithDefaults < Props , Defaults >
158
+ ) : PropsWithDefaults < T , Defaults , BKeys >
142
159
143
160
// make them global
144
161
type _defineProps = typeof defineProps
0 commit comments