typescript 参数声明,参数之前相互依赖,实例化时能明确传入参数,a参数的取值,依赖b参数的值

// 声明类型
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 });

 

posted @ 2023-12-11 17:35  程序員劝退师  阅读(15)  评论(0编辑  收藏  举报