Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type definitions #19

Open
DimaGashko opened this issue Mar 4, 2020 · 4 comments
Open

Type definitions #19

DimaGashko opened this issue Mar 4, 2020 · 4 comments

Comments

@DimaGashko
Copy link

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.

@cinderblock
Copy link

cinderblock commented Apr 18, 2020

@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.

@DimaGashko
Copy link
Author

DimaGashko commented Apr 18, 2020

@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

@cinderblock
Copy link

@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;

@ghiscoding
Copy link

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants