diff --git a/components/_util/props-util.js b/components/_util/props-util/index.js similarity index 100% rename from components/_util/props-util.js rename to components/_util/props-util/index.js diff --git a/components/_util/props-util/initDefaultProps.ts b/components/_util/props-util/initDefaultProps.ts new file mode 100644 index 000000000..3b1a58e9a --- /dev/null +++ b/components/_util/props-util/initDefaultProps.ts @@ -0,0 +1,24 @@ +import { VueTypeValidableDef, VueTypeDef } from 'vue-types'; + +const initDefaultProps = ( + propTypes: T, + defaultProps: { + [K in keyof T]?: T[K] extends VueTypeValidableDef + ? U + : T[K] extends VueTypeDef + ? U + : any; + }, +): 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;