// 声明类型 export interface Iform{ Input: { text: string; }, Select: { options: any[]; }, Radio: { checkd: boolean; }, Switch: { checked: boolean; }, } // 声明函数定义函数参数key,form,form取值,校验依赖于key export function getFromVaild<T extends keyof Iform>(key: T, form: Iform[T]) { } // 使用时,创建实例后,输入一个参数key后,第二个参数会自动弹出提示 const input = getFromVaild('Input', { text: '' }); const select = getFromVaild('Select', { options: [] }); const radio = getFromVaild('Radio', { checkd: false });
标签:typescript,const,化时,form,getFromVaild,参数,key,取值 From: https://www.cnblogs.com/cxyqts/p/17894997.html