From 88d3b6010b14b2cc07f8c93c80655b36577dfbb3 Mon Sep 17 00:00:00 2001 From: Amour1688 Date: Tue, 13 Oct 2020 22:57:56 +0800 Subject: [PATCH] chore: initDefaultProps type --- .../{props-util.js => props-util/index.js} | 0 .../_util/props-util/initDefaultProps.ts | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+) rename components/_util/{props-util.js => props-util/index.js} (100%) create mode 100644 components/_util/props-util/initDefaultProps.ts 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;