2021-06-26 01:35:40 +00:00
|
|
|
import type { PropType } from 'vue';
|
|
|
|
import type { VueTypeValidableDef, VueTypeDef } from 'vue-types';
|
2020-10-13 14:57:56 +00:00
|
|
|
|
|
|
|
const initDefaultProps = <T>(
|
2020-10-14 15:12:51 +00:00
|
|
|
types: T,
|
2020-10-13 14:57:56 +00:00
|
|
|
defaultProps: {
|
|
|
|
[K in keyof T]?: T[K] extends VueTypeValidableDef<infer U>
|
|
|
|
? U
|
|
|
|
: T[K] extends VueTypeDef<infer U>
|
|
|
|
? U
|
2020-10-28 07:46:00 +00:00
|
|
|
: T[K] extends { type: PropType<infer U> }
|
|
|
|
? U
|
2020-10-13 14:57:56 +00:00
|
|
|
: any;
|
|
|
|
},
|
|
|
|
): T => {
|
2021-09-07 06:16:46 +00:00
|
|
|
const propTypes: T = { ...types };
|
2020-10-13 14:57:56 +00:00
|
|
|
Object.keys(defaultProps).forEach(k => {
|
|
|
|
const prop = propTypes[k] as VueTypeValidableDef;
|
|
|
|
if (prop) {
|
2021-09-25 08:51:32 +00:00
|
|
|
if (prop.type || prop.default) {
|
|
|
|
prop.default = defaultProps[k];
|
|
|
|
} else if (prop.def) {
|
|
|
|
prop.def(defaultProps[k]);
|
|
|
|
} else {
|
|
|
|
propTypes[k] = { type: prop, default: defaultProps[k] };
|
|
|
|
}
|
2020-10-13 14:57:56 +00:00
|
|
|
} else {
|
|
|
|
throw new Error(`not have ${k} prop`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return propTypes;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default initDefaultProps;
|