Bas Hamer
Bas Hamer

Reputation: 324

typescript inline method syntax for creating anonymous types

I'm looking at the following code

        var results = _(items).groupBy((i) => i.key))
            .map((group, key) => { return { key: key, group: group } })
            .value();

line 2 bothers the hell out of me as I feel that I should be able to do it like line 1 (the one that does not use a return). What would be the syntax to do that?

Upvotes: 0

Views: 92

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250036

The problem is that the syntax is ambiguous between an arrow function that returns an object literal and one that has a block, the spec decided to just interpret p => { as an arrow function with block not an arrow function with an object literal. You can easily get around this with some () :

    var results = _(items).groupBy((i) => i.key))
        .map((group, key) => ({ key: key, group: group }))
        .value();

Upvotes: 1

Related Questions