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

Update deep-diff.d.ts #11637

Closed
wants to merge 5 commits into
base: master
from

Conversation

Projects
None yet
7 participants
@forabi
Contributor

forabi commented Sep 30, 2016

Documentation: https://github.com/flitbit/diff

Improvements compared to the current typings:

  • Introduces and uses a DiffableObject<T> type instead of a generic object, this allows VS Code to understand what the result of a function call to diff will be.
  • Introduces a BaseDiff<T> interface which is extended for each kind of diffs. This allows VS Code to perform control flow analysis in TypeScript >= 2.0.
  • For ArrayDiff, the first item in the path array is a number
  • Add descriptions for all functions and types
  • diff() may return undefined for indentical objects

Example

import deepDiff from 'deep-diff';
// or import { diff as deepDiff } from 'deep-diff';

type landmark = { [id: string]: GeometricalObject } | { };

const diffs = deepDiff(
  this.props.landmarks,
  nextProps.landmarks
); // diffs is of type Diff<GeometricalObject>[] or undefined;

diffs.forEach(diff => /* do something */) // Error: diffs is possibly undefined

if (diffs) {
  // diffs is now of type Diff<GeometricalObject>[]
  shouldRerender = true;
  diffs.forEach(diff => {
    // diff is of type Diff<GeometricalObject>
    if (diff.kind === 'N') {
      // diff is now of type NewDiff<GeometricalObject>
      // rhs is of type GeometricalObject;
      const object = geometricalObjectToFabricObject(diff.rhs, diff.path[0]);


      // Property lhs is undefined for diffs of kind N
      diff.lhs // Error: Property 'lhs' does not exist on type 'NewDiff<GeometricalObject>'
      if (object) {
        // diff.path is of type string[];
        objectMap.set(diff.path[0], object);
        landmarksGroup.add(object);
      }
    } else if (diff.kind === 'E') {
      // diff is of type EditDiff<GeometricalObject>
      // diff has properties: lhs, rhs
      objectMap.get(diff.path[0]).set(diff.path[1], diff.rhs);
    } else if (diff.kind === 'D') {
      // diff is of type DeleteDiff<GeometricalObject>
      // Property rhs is undefined for diffs of kind N
      diff.rhs // Error: Property 'rhs' does not exist on type 'NewDiff<GeometricalObject>'
      objectMap.get(diff.path[1]).remove();
      objectMap.delete(diff.path[0]);
    } else if (diff.kind === 'A') {
      // diff is of type ArrayDiff<GeometricalObject>
      // diff has properties: index, item
      // diff.path[0] is of type number
      // diff.path[1] is of type string
    }
  });
}
Update deep-diff.d.ts
Improvements compared to the current typings:

* Introduces and uses a DiffableObject<T> instead of a generic object, this allows VS Code to understand what the result of a function call to `diff` will be.
* Introduces a BaseDiff<T> interface which is extended for each kind of Diffs, this allows VS Code to perform control flow analysis in TypeScript >= 2.0.
* For ArrayDiff, the first item in the path array is a number
* Add descriptions for all functions and types

```typescript
import deepDiff from 'deep-diff';
// or import { diff as deepDiff } from 'deep-diff';

type landmark = { [id: string]: GeometricalObject } | { };

const diffs = deepDiff(
  this.props.landmarks,
  nextProps.landmarks
); // diffs is of type Diff<GeometricalObject>[];
if (diffs) {
  shouldRerender = true;
  diffs.forEach(diff => {
    // diff is of type Diff<GeometricalObject>
    if (diff.kind === 'N') {
      // diff is now of type NewDiff<GeometricalObject>
     // rhs is of type GeometricalObject;
      const object = geometricalObjectToFabricObject(diff.rhs, diff.path[0]);
      

      // Property lhs is undefined for diffs of kind N
      diff.lhs // Error: Property 'lhs' does not exist on type 'NewDiff<GeometricalObject>'
      if (object) {
        // diff.path is of type string[];
        objectMap.set(diff.path[0], object);
        landmarksGroup.add(object);
      }
    } else if (diff.kind === 'E') {
      // diff is of type EditDiff<GeometricalObject>
      // diff has properties: lhs, rhs
      objectMap.get(diff.path[0]).set(diff.path[1], diff.rhs);
    } else if (diff.kind === 'D') {
      // diff is of type DeleteDiff<GeometricalObject>
     // Property rhs is undefined for diffs of kind N
      diff.rhs // Error: Property 'rhs' does not exist on type 'NewDiff<GeometricalObject>'
      objectMap.get(diff.path[1]).remove();
      objectMap.delete(diff.path[0]);
    }  else if (diff.kind === 'A') {
        // diff is of type ArrayDiff<GeometricalObject>
        // diff has properties: index, item
        // diff.path[0] is of type number
        // diff.path[1] is of type string
     }
  });
```
@dt-bot

This comment has been minimized.

Member

dt-bot commented Sep 30, 2016

deep-diff/deep-diff.d.ts

to author (@ZauberNerd). Could you review this PR?
👍 or 👎?

Checklist

  • pass the Travis CI test?
@@ -1,42 +1,142 @@
// Type definitions for deep-diff
// Project: https://github.com/flitbit/diff/
// Definitions by: ZauberNerd <https://github.com/ZauberNerd/>
// Definitions by: forabi <https://github.com/forabi/>

This comment has been minimized.

@vvakame

vvakame Oct 3, 2016

Member

don't remove original author's name.

This comment has been minimized.

@forabi

forabi Oct 15, 2016

Contributor

I restored the original author

@ZauberNerd

This comment has been minimized.

Contributor

ZauberNerd commented Oct 19, 2016

👍 from my side - but the travis build is still failing.

@yuit

This comment has been minimized.

Contributor

yuit commented Nov 8, 2016

@forabi Thank you for the PR, could you fix the CI-failure? once that is done I will merge it in

forabi added some commits Nov 9, 2016

@forabi

This comment has been minimized.

Contributor

forabi commented Nov 9, 2016

I realized my usage of deep-diff was a very specific case. I had to do diffing of objects that could be represented as a map, i.e

{ [id: string]: GeometricalObject };

The library can do diffs on objects with properties assigned to different types of objects, so I don't think making the tests pass for my use case is a good idea.

Hopefully, once mappable and partial types land in TS, I will fix this.

@paulvanbrenk

This comment has been minimized.

Contributor

paulvanbrenk commented Nov 30, 2016

@forabi can you fix the Travis issues? This may require you to merge the base branch in.
If no response in 7 days, I'll close this PR.

@andy-ms

This comment has been minimized.

Contributor

andy-ms commented Dec 8, 2016

@forabi Mappable types are in 2.1 now, so please make a PR to the types-2.0 branch. Instructions here.

@andy-ms andy-ms closed this Dec 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment