Recently, I came across TypeScript and decided to convert my current JavaScript code to TypeScript.
In one of my functions, I extract information from a string (data
), store it in a JSON object (json
), and then return the object. However, when working with TypeScript and not specifying a return type, an error pops up in Eclipse:
No best common type exists among return expressions
Adding the any
return type makes the error disappear, but I feel like this is too general of a solution. I've searched for a specific "json" or "object" type without success.
So my question is: what should be the appropriate return type for this function?
Below is the function in question:
function formDataFormatter(data: string) { // or (data: string): any
// final json object
var json = {
y: {
"vars": [],
"smps": [],
"data": []
}
};
// ...
// processing data...
// ...
// populate new variables in JSON (values are placeholders)
json.y.data = ["data"];
json.y.smps = ["smps"];
json.y.vars = ["vars"];
return json;
};