Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return JSON object with TypeScript function

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;

};
like image 641
Hulothe Avatar asked Apr 20 '17 15:04

Hulothe


People also ask

How do you return an object from a function in TypeScript?

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.

How do you get a specific value from a JSON object in TypeScript?

To parse a JSON string in TypeScript, you can use JSON. parse().

How do I create a JSON object in TypeScript?

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.

How do you parse an object in TypeScript?

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.

How to create a JSON object in typescript?

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.

How to set the return type of a function in typescript?

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.

What is the use of typescript in JavaScript?

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.

How do I declare a function with an object return type?

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.


1 Answers

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;
}
like image 61
Nitzan Tomer Avatar answered Sep 18 '22 11:09

Nitzan Tomer