Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to support unlimited parameters in extend function from lodash in typing of typescript?

I using extend from lodash to "concat" the objects in the arguments like so:

import { extend } from 'lodash';

const foo1 = { item: 1 };
const foo2 = { item: 1 };
const foo3 = { item: 1 };
const foo4 = { item: 1 };
const foo5 = { item: 1 };
const foo6 = { item: 1 };

const b = extend(foo1, foo2, foo3, foo4, foo5, foo6);

The problem is after 5 parameters the type of b turn to unknown.

I mean when it just: extend(foo1, foo2, foo3, foo4, foo5); the type is correct.
But when it extend(foo1, foo2, foo3, foo4, foo5, foo6); turn to unknown.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I fix that to support unlimited parameters?

stackblitz

  • Please note, I don’t want to use spread operator because extend not work exactly
    like spread operator. I passing to extend Proxy object which not handling so good with spread operator.

>Solution :

If you look at the types for lodash, you’ll see this for extend:

interface LoDashStatic {
    /**
     * @see _.extend
     */
    extend<TObject, TSource>(object: TObject, source: TSource): TObject & TSource;
    /**
     * @see _.extend
     */
    extend<TObject, TSource1, TSource2>(object: TObject, source1: TSource1, source2: TSource2): TObject & TSource1 & TSource2;
    /**
     * @see _.extend
     */
    extend<TObject, TSource1, TSource2, TSource3>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3): TObject & TSource1 & TSource2 & TSource3;
    /**
     * @see _.extend
     */
    extend<TObject, TSource1, TSource2, TSource3, TSource4>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3, source4: TSource4): TObject & TSource1 & TSource2 & TSource3 & TSource4;
    /**
     * @see _.extend
     */
    extend<TObject>(object: TObject): TObject;
    /**
     * @see _.extend
     */
    extend<TResult>(object: any, ...otherArgs: any[]): TResult;
}

Notice how there are explicit overloads for one, two, three, and four source parameters, but then a catch-all for more than that. If you want to support more than that, you could add to the types for lodash to do more overloads, but beware there’s an upper limit.

You could always combine calls so you’re staying within those explicit overloads for each individual call:

extend(foo1, extend(foo2, foo3, foo4, foo5, foo6));

Playground link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading