@@ -81,7 +81,10 @@ export function defineProps<
81
81
PP extends ComponentObjectPropsOptions = ComponentObjectPropsOptions
82
82
> ( props : PP ) : Prettify < Readonly < ExtractPropTypes < PP > > >
83
83
// overload 3: typed-based declaration
84
- export function defineProps < TypeProps > ( ) : DefineProps < TypeProps >
84
+ export function defineProps < TypeProps > ( ) : DefineProps <
85
+ TypeProps ,
86
+ BooleanKey < TypeProps >
87
+ >
85
88
// implementation
86
89
export function defineProps ( ) {
87
90
if ( __DEV__ ) {
@@ -90,8 +93,8 @@ export function defineProps() {
90
93
return null as any
91
94
}
92
95
93
- type DefineProps < T > = Readonly < T > & {
94
- readonly [ K in BooleanKey < T > ] -?: boolean
96
+ type DefineProps < T , BKeys extends keyof T > = Readonly < T > & {
97
+ readonly [ K in BKeys ] -?: boolean
95
98
}
96
99
97
100
type BooleanKey < T , K extends keyof T = keyof T > = K extends any
@@ -281,26 +284,27 @@ interface DefineModelOptions {
281
284
type NotUndefined < T > = T extends undefined ? never : T
282
285
283
286
type InferDefaults < T > = {
284
- [ K in keyof T ] ?: InferDefault < T , NotUndefined < T [ K ] > >
287
+ [ K in keyof T ] ?: InferDefault < T , T [ K ] >
285
288
}
286
289
287
- type InferDefault < P , T > = T extends
288
- | null
289
- | number
290
- | string
291
- | boolean
292
- | symbol
293
- | Function
294
- ? T | ( ( props : P ) => T )
295
- : ( props : P ) => T
296
-
297
- type PropsWithDefaults < Base , Defaults > = Base & {
298
- [ K in keyof Defaults ] : K extends keyof Base
290
+ type NativeType = null | number | string | boolean | symbol | Function
291
+
292
+ type InferDefault < P , T > =
293
+ | ( ( props : P ) => T & { } )
294
+ | ( T extends NativeType ? T : never )
295
+
296
+ type PropsWithDefaults <
297
+ T ,
298
+ Defaults extends InferDefaults < T > ,
299
+ BKeys extends keyof T
300
+ > = Omit < T , keyof Defaults > & {
301
+ [ K in keyof Defaults ] -? : K extends keyof T
299
302
? Defaults [ K ] extends undefined
300
- ? Base [ K ]
301
- : NotUndefined < Base [ K ] >
303
+ ? T [ K ]
304
+ : NotUndefined < T [ K ] >
302
305
: never
303
- }
306
+ } & { readonly [ K in BKeys ] -?: boolean }
307
+
304
308
/**
305
309
* Vue `<script setup>` compiler macro for providing props default values when
306
310
* using type-based `defineProps` declaration.
@@ -321,10 +325,14 @@ type PropsWithDefaults<Base, Defaults> = Base & {
321
325
*
322
326
* @see {@link https://vuejs.org/guide/typescript/composition-api.html#typing-component-props }
323
327
*/
324
- export function withDefaults < Props , Defaults extends InferDefaults < Props > > (
325
- props : Props ,
328
+ export function withDefaults <
329
+ T ,
330
+ BKeys extends keyof T ,
331
+ Defaults extends InferDefaults < T >
332
+ > (
333
+ props : DefineProps < T , BKeys > ,
326
334
defaults : Defaults
327
- ) : PropsWithDefaults < Props , Defaults > {
335
+ ) : PropsWithDefaults < T , Defaults , BKeys > {
328
336
if ( __DEV__ ) {
329
337
warnRuntimeUsage ( `withDefaults` )
330
338
}
0 commit comments