EternallyCurious
EternallyCurious

Reputation: 2415

Typescript syntax for passing functions

I am calling a Typescript function passing a string and a function as parameters to it. But I have an error possibly because of syntactical mistakes.

Calling code:

send(
            {
                num: value,
                file: fileData
            },
            function(responseData){
                alert('Successfully uploaded : ' + responseData + " and received ");
            }
        );

Called function:

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

Error:

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.

Upvotes: 0

Views: 809

Answers (1)

Andreas Rossberg
Andreas Rossberg

Reputation: 36078

No syntax error. The error message says it all: the object you pass (as the first argument) does not have the right type. (The function expects a String.)

Upvotes: 1

Related Questions