29 lines
685 B
TypeScript
29 lines
685 B
TypeScript
import { PropType } from 'vue';
|
|
import { VueTypeValidableDef, VueTypeDef } from 'vue-types';
|
|
|
|
const initDefaultProps = <T>(
|
|
types: T,
|
|
defaultProps: {
|
|
[K in keyof T]?: T[K] extends VueTypeValidableDef<infer U>
|
|
? U
|
|
: T[K] extends VueTypeDef<infer U>
|
|
? U
|
|
: T[K] extends { type: PropType<infer U> }
|
|
? U
|
|
: any;
|
|
},
|
|
): T => {
|
|
const propTypes: T = { ...types } as T;
|
|
Object.keys(defaultProps).forEach(k => {
|
|
const prop = propTypes[k] as VueTypeValidableDef;
|
|
if (prop) {
|
|
prop.default = defaultProps[k];
|
|
} else {
|
|
throw new Error(`not have ${k} prop`);
|
|
}
|
|
});
|
|
return propTypes;
|
|
};
|
|
|
|
export default initDefaultProps;
|