类型“T”的参数不能赋给类型“string”的参数

报错代码如下

export const cycTest=(value:any,testName:string):boolean=>testObject[testName].test(value);
export const cycTestLabel=<T>(value:any,testName:T,label:T):(boolean|Error)=>{
  return !cycTest(value,testName)?new Error(`${label}格式错误`):true
};

报错信息如下

 解决办法记录

直接<T extend string>可以直接解决

export const cycTest=(value:any,testName:string):boolean=>testObject[testName].test(value);
export const cycTestLabel=<T extends string>(value:any,testName:T,label:T):(boolean|Error)=>{
  return !cycTest(value,testName)?new Error(`${label}格式错误`):true
};

博主的wx公众号:"程序员野区"菜单栏有福利

猜你喜欢

转载自blog.csdn.net/xuelang532777032/article/details/131900907