A parameter of type 'T' cannot be assigned to a parameter of type 'string'

The error code is as follows

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
};

The error message is as follows

 Solution record

Direct <T extend string> can be solved directly

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
};

Blogger's wx public account: " Programmer Wild Area" has benefits in the menu bar

Guess you like

Origin blog.csdn.net/xuelang532777032/article/details/131900907