Ad

Flow Type And Merging Json Objects

- 1 answer

Assume code of the following kind (e.g. using lodash or explicitly like here):

function extend(base, overwrite) {
    for (var key in overwrite)
        base[key] = overwrite[key];
    return base;
}

var first = extend({
    a: 1
}, {
    b: 2
});

var second = extend({
    c: 3
}, {
    d: 4
});

console.log(first.a + first.b + second.c + second.d);

How can I explain to Flowtype that this is actually fine?

Ad

Answer

Try putting this just above function declaration:

declare function extend<A, B>(a: A, b: B): A & B
Ad
source: stackoverflow.com
Ad