最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return JSON object with TypeScript function - Stack Overflow

programmeradmin2浏览0评论

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;

};

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;

};
Share Improve this question asked Apr 20, 2017 at 15:19 HulotheHulothe 7641 gold badge6 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 15

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;
}
发布评论

评论列表(0)

  1. 暂无评论