ant-design-vue/components/_util/props-util/initDefaultProps.ts

29 lines
685 B
TypeScript
Raw Normal View History

2020-10-28 07:46:00 +00:00
import { PropType } from 'vue';
2020-10-13 14:57:56 +00:00
import { VueTypeValidableDef, VueTypeDef } from 'vue-types';
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 => {
const propTypes: T = { ...types } as T;
2020-10-13 14:57:56 +00:00
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;