我正在调用一个Typescript函数,将一个字符串和一个函数作为参数传递给它。但是我可能因语法错误而出错。
致电代码:
send(
{
num: value,
file: fileData
},
function(responseData){
alert('Successfully uploaded : ' + responseData + " and received ");
}
);
被叫功能:
function send(data : String, success: Function){
$.ajax({
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
url: '/testData',
success: function (responseData) {
return JSON.parse(responseData);
success(responseData);
},
error: function(error){
return null;
}
});
}
错误:
C:/Users/Me/AppData/Roaming/npm/tsc.cmd --sourcemap Start.ts --module commonjs --out main.js
C:/Users/Me/WebstormProjects/Core/public/javascripts/Start.ts(54,9): error TS2082: Supplied parameters do not match any signature of call target:
Type '{ num: any; file: any; }' is missing property 'charAt' from type 'String'.
C:/Users/Me/WebstormProjects/Core/public/javascripts/Start.ts(54,9): error TS2087: Could not select overload for 'call' expression.
答案 0 :(得分:1)
没有语法错误。错误消息说明了一切:您传递的对象(作为第一个参数)没有正确的类型。 (该函数需要一个String。)