I discovered TypeScript recently and I try to convert my existing JavaScript code to TypeScript.
I have a function that retrieves information from a string (data
), puts it in a JSON object (json
) and returns it. But when using TypeScript and not specifying a return type, I get the following error in Eclipse:
No best common type exists among return expressions
It disappears when I add any
return type, but I think this isn't a good solution (too generic). And I couldn't find a "json" or "object" type.
My question is: what return type should I use?
Here is the function:
function formaterDonnees(data: string) { // or (data: string): any
// final json object
var json = {
y: {
"vars": [],
"smps": [],
"data": []
}
};
// ...
// processing data...
// ...
// put new variables in JSON (not real values below)
json.y.data = ["data"];
json.y.smps = ["smps"];
json.y.vars = ["vars"];
return json;
};
To declare a function with an object return type, set the return type of the function to an object right after the function's parameter list, e.g. function getObj(): {name: string;} {} . If the return type of the function is not set, TypeScript will infer it.
To parse a JSON string in TypeScript, you can use JSON. parse().
obj: any; //new object declaration this. obj = { "col1":{"Attribute1": "value1", "Attribute2": "value2", "Attribute3": "value3"}, "col2":{"Attribute1": "value4", "Attribute2": "value5", "Attribute3": "value6"}, "col3":{"Attribute1": "value7", "Attribute2": "value8", "Attribute3": "value9"} } this. output.
To parse a JSON object to a TypeScript object, we use the JSON. parse method. interface Employee { departmentId: number; permissionsId: number; maxWorkHours: number; employeeId: number; firstName: string; lastName: string; } const jsonObj: any = JSON.
We can create a JSON object in TYPESCRIPT dynamically. There are two types of objects in TypeScript. A Plain Object that we achieve with json.parse () which results in the plain object, and the TypeScript Class, which returns the Class Object.
If the return type of the function is not set, TypeScript will infer it. You can set the return type of a function right after its parameter list. The examples above show how to set the return type of a function to an object that contains a name and age properties.
The TypeScript comes up with the functionality of working with JSON Type data. JSON being the JavaScript Object Notation, is used to make a data model that is easy to write and read.
To declare a function with an object return type, set the return type of the function to an object right after the function's parameter list, e.g. function getObj (): {name: string;} {}. If the return type of the function is not set, TypeScript will infer it. You can set the return type of a function right after its parameter list.
You can indeed specify that you return object
(new to typescript 2.2), but you can create a type for your return value:
type MyReturnTypeItem = {
vars: string[];
smps: string[];
data: string[];
}
type MyReturnType = {
[name: string]: MyReturnTypeItem;
}
function formaterDonnees(data: string): MyReturnType {
var json = {
y: {
"vars": [],
"smps": [],
"data": []
}
};
// put new variables in JSON (not real values below)
json.y.data = ["data"];
json.y.smps = ["smps"];
json.y.vars = ["vars"];
return json;
};
(code in playground)
Also, while I used type alias you can do the same with interfaces:
interface MyReturnTypeItem {
vars: string[];
smps: string[];
data: string[];
}
interface MyReturnType {
[name: string]: MyReturnTypeItem;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With