assign-deep icon indicating copy to clipboard operation
assign-deep copied to clipboard

Type definitions

Open DimaGashko opened this issue 5 years ago • 4 comments

I've just decided to use this cool lib. I found some similar libs and I think this one is better but there's one small point - type definitions.

Can you add jsDoc comment to the function, or add this lib to https://definitelytyped.org/? If you do that, your lib will be the best.

DimaGashko avatar Mar 04 '20 20:03 DimaGashko

@DimaGashko If you have some type definitions that would work, you can just add them to DefinitelyTyped yourself.

However, the best would be a new minor version here that adds the types directly into this package. DefinitelyTyped is intended to fill the gap that where package maintainers don't add the types to their own packages.

cinderblock avatar Apr 18 '20 00:04 cinderblock

@cinderblock type definitions that would work are something like type definitions for native Object.assign

interface ObjectConstructor {
    /**
     * Copy the values of all of the enumerable own properties from one or more source objects to a
     * target object. Returns the target object.
     * @param target The target object to copy to.
     * @param source The source object from which to copy properties.
     */
    assign<T, U>(target: T, source: U): T & U;

    /**
     * Copy the values of all of the enumerable own properties from one or more source objects to a
     * target object. Returns the target object.
     * @param target The target object to copy to.
     * @param source1 The first source object from which to copy properties.
     * @param source2 The second source object from which to copy properties.
     */
    assign<T, U, V>(target: T, source1: U, source2: V): T & U & V;

    /**
     * Copy the values of all of the enumerable own properties from one or more source objects to a
     * target object. Returns the target object.
     * @param target The target object to copy to.
     * @param source1 The first source object from which to copy properties.
     * @param source2 The second source object from which to copy properties.
     * @param source3 The third source object from which to copy properties.
     */
    assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;

    /**
     * Copy the values of all of the enumerable own properties from one or more source objects to a
     * target object. Returns the target object.
     * @param target The target object to copy to.
     * @param sources One or more source objects from which to copy properties
     */
    assign(target: object, ...sources: any[]): any;

image

Or with jsDoc: image

Unfortunately I don't have enough time to add it to type definitions

DimaGashko avatar Apr 18 '20 02:04 DimaGashko

@DimaGashko I was thinking about this a bit today. It looks like TypeScript's Intersection Types (&) do the correct deep merging of the types, so this should work.

I wonder if this would be a better generic type:

assign<T extends {}>(...args: RecursivePartial<T>}: T;

cinderblock avatar Apr 18 '20 09:04 cinderblock

last suggestion didn't work with latest TypeScript 4.5 but looking at other DefinitelyType declarations like this one object.assign, we can come up with pretty much the same and provides better typing I think (I had to remove the prefix of declare function to function to get rid of the ambient context error thrown by latest TS and now it's all good).

declare module 'assign-deep' {
  function assign<T, U>(target: T, source: U[]): T & U;
  function assign<T, U, V>(target: T, source1: U, source2: V): T & U & V;
  function assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
  function assign<T, U, V, W, Q>(target: T, source1: U, source2: V, source3: W, source4: Q): T & U & V & W & Q;
  function assign<T, U, V, W, Q, R>(target: T, source1: U, source2: V, source3: W, source4: Q, source5: R): T & U & V & W & Q & R;
  function assign(target: any, ...sources: any[]): any;
  namespace assign { }
  export = assign;
}

ghiscoding avatar Feb 09 '22 19:02 ghiscoding