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

Fix type/docs for VivView(er) and Consumers. #407

Merged
merged 7 commits into from
Apr 5, 2021

Conversation

ilan-gold
Copy link
Collaborator

@ilan-gold ilan-gold commented Apr 1, 2021

Fixes #406

Background

In #397 we introduced types via jsdoc but failed to correctly type the VivViewer.

Change List

  • Move the docstring from the class declaration to the constructor

Checklist

  • Update JSdoc types if there is any API change.
  • Make sure Avivator works as expected with your change.

@ilan-gold ilan-gold force-pushed the ilan-gold/fix_viv_viewer_type branch 2 times, most recently from 7703fcf to ae7e001 Compare April 1, 2021 20:18
@ilan-gold ilan-gold force-pushed the ilan-gold/fix_viv_viewer_type branch from ae7e001 to beb6572 Compare April 1, 2021 20:23
@ilan-gold ilan-gold changed the title Fix type/docs for VivViewer. Fix type/docs for VivView(er) and Consumers. Apr 1, 2021
@ilan-gold
Copy link
Collaborator Author

@andreasg123 Can you give this a whirl? The .ts looks more correct now and // @ts-check doesn't seem to give any bad errors for any of the React components anymore. Even if this doesn't work, this is definitely an improvement in the types so I'd merge this anyway.

export default VivViewer;
export type ViewStateChange = (event: Object) => any;
export type Hover = (info: Object, event: Object) => any;
export type HandleValue = (valueArray: Array<number>) => any;
export type HandleCoordinate = (coordnate: Object) => any;
export type HoverHooks = {
    handleValue: HandleValue;
    handleCoordinate: HandleCoordinate;
};
export type LayerProps = {
    /**
     * Props for the layers in each view.
     */
    layerProps: any[];
    /**
     * Whether or not to randomize which view goes first (for dynamic rendering of multiple linked views).
     */
    randomize?: boolean | undefined;
    /**
     * Various `VivView`s to render.
     */
    views: Array<import('../views').VivView>;
    /**
     * List of objects like [{ target: [x, y, 0], zoom: -zoom, id: 'left' }, { target: [x, y, 0], zoom: -zoom, id: 'right' }]
     */
    viewStates: Array<object>;
    /**
     * Callback that returns the deck.gl view state (https://deck.gl/docs/api-reference/core/deck#onviewstatechange).
     */
    onViewStateChange?: ViewStateChange | undefined;
    /**
     * Callback that returns the picking info and the event (https://deck.gl/docs/api-reference/core/layer#onhover
     * https://deck.gl/docs/developer-guide/interactivity#the-picking-info-object)
     */
    onHover?: Hover | undefined;
    /**
     * Object including utility hooks - an object with key handleValue like { handleValue: (valueArray) => {}, handleCoordinate: (coordinate) => {} } where valueArray
     * has the pixel values for the image under the hover location and coordinate is the coordinate in the image from which the values are picked.
     */
    hoverHooks?: HoverHooks | undefined;
};
/**
 * @callback ViewStateChange
 * @param {Object} event
 * @ignore
 */
/**
 * @callback Hover
 * @param {Object} info
 * @param {Object} event
 * @ignore
 */
/**
 * @callback HandleValue
 * @param {Array.<number>} valueArray pixel values for the image under the hover location
 * @ignore
 */
/**
 * @callback HandleCoordinate
 * @param {Object} coordnate The coordinate in the image from which the values are picked.
 * @ignore
 */
/**
 * @typedef HoverHooks
 * @type {object}
 * @property {HandleValue} handleValue
 * @property {HandleCoordinate} handleCoordinate
 * @ignore
 */
/**
 * @typedef LayerProps
 * @type {object}
 * @property {Array} layerProps  Props for the layers in each view.
 * @property {boolean} [randomize] Whether or not to randomize which view goes first (for dynamic rendering of multiple linked views).
 * @property {Array.<import('../views').VivView>} views Various `VivView`s to render.
 * @property {Array.<object>} viewStates List of objects like [{ target: [x, y, 0], zoom: -zoom, id: 'left' }, { target: [x, y, 0], zoom: -zoom, id: 'right' }]
 * @property {ViewStateChange} [onViewStateChange] Callback that returns the deck.gl view state (https://deck.gl/docs/api-reference/core/deck#onviewstatechange).
 * @property {Hover} [onHover] Callback that returns the picking info and the event (https://deck.gl/docs/api-reference/core/layer#onhover
 *     https://deck.gl/docs/developer-guide/interactivity#the-picking-info-object)
 * @property {HoverHooks} [hoverHooks] Object including utility hooks - an object with key handleValue like { handleValue: (valueArray) => {}, handleCoordinate: (coordinate) => {} } where valueArray
 * has the pixel values for the image under the hover location and coordinate is the coordinate in the image from which the values are picked.
 */
/**
 * @type {{ new(props: LayerProps): { props: LayerProps, state: any } }}
 */
declare const VivViewer: new (props: LayerProps) => {
    props: LayerProps;
    state: any;
};

@andreasg123
Copy link
Contributor

@ilan-gold, I'm now getting this error:

Type error: 'VivViewer' cannot be used as a JSX component.
  Its instance type '{ props: LayerProps; state: any; }' is not a valid JSX element.
    Type '{ props: LayerProps; state: any; }' is missing the following properties from type 'ElementClass': render, context, setState, forceUpdate, refs

  136 |   }
  137 |   return (
> 138 |     <VivViewer
      |      ^
  139 |       layerProps={layerProps}
  140 |       views={views}
  141 |       viewStates={viewStates}

@andreasg123
Copy link
Contributor

andreasg123 commented Apr 1, 2021

Apparently, inheritance can be described like this: microsoft/TypeScript#20077
The parent class should be PureComponent<LayerProps> if JSDoc supports generics.

Definition for PureComponent: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/v16/index.d.ts#L508

@ilan-gold
Copy link
Collaborator Author

@andreasg123 I couldn't get the generic or the extension working, but I did try wrapping it in a functional component and then documenting that. Seems to work for me in Avivator. Let me know!

@andreasg123
Copy link
Contributor

This mostly works. I have to figure out how to produce the correct ViewStateChange type:

Type error: Type 'ViewStateChange | undefined' is not assignable to type 'import("/Users/andreas.girgensohn/go/src/github.com/10XDev/ziggy/client/node_modules/@hms-dbmi/viv/dist/viewers/VivViewer").ViewStateChange | undefined'.
  Type 'ViewStateChange' is not assignable to type 'import("/Users/andreas.girgensohn/go/src/github.com/10XDev/ziggy/client/node_modules/@hms-dbmi/viv/dist/viewers/VivViewer").ViewStateChange'.
    Types of parameters '__0' and 'event' are incompatible.
      The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
        Type 'Object' is missing the following properties from type '{ viewId: any; viewState: any; oldViewState: any; }': viewId, viewState, oldViewState

  141 |       viewStates={viewStates}
  142 |       hoverHooks={hoverHooks}
> 143 |       onViewStateChange={onViewStateChange}
      |       ^
  144 |     />
  145 |   );
  146 | };

I'm using this definition:

type ViewStateChange = ({
  viewId,
  viewState,
  oldViewState,
}: {
  viewId: any;
  viewState: any;
  oldViewState: any;
}) => void;

@andreasg123
Copy link
Contributor

andreasg123 commented Apr 2, 2021

I think that I'm good to go. I fixed the previous error like this. That didn't work.

onViewStateChange={(onViewStateChange as unknown) as VivViewer.ViewStateChange}

This worked:

type ViewStateChange = (event: Object) => void;

It shouldn't be difficult to fix this error in my project:

Type error: Type '{ layerProps: { loader: ZarrPixelSource<["t", "c", "z"] | ["c", "t", "z"] | ["z", "t", "c"] | ["t", "z", "c"] | ["z", "c", "t"] | ["c", "z", "t"]>[] | TiffPixelSource<[...] | ... 4 more ... | [...]>[] | undefined; ... 15 more ...; lensBorderRadius: number; }[]; views: any[]; viewStates: any[]; hoverHooks: any; onVie...' is missing the following properties from type '{ layerProps: any[]; randomize: boolean | undefined; views: VivView[]; viewStates: object[]; onViewStateChange: ViewStateChange | undefined; onHover: Hover | undefined; hoverHooks: HoverHooks | undefined; }': randomize, onHover

  136 |   }
  137 |   return (
> 138 |     <VivViewer
      |      ^
  139 |       layerProps={layerProps}
  140 |       views={views}
  141 |       viewStates={viewStates}

Thank you very much for the quick response.

@andreasg123
Copy link
Contributor

Could you please add this to ImageLayer.js (copied from MultiscaleImageLayer.js)?

 * @param {String} props.id Unique identifier for this layer.

The following better describes VivView and allows for easier subclassing. This JSDoc documentation may be helpful for describing argument types: https://jsdoc.app/tags-type.html

diff --git a/src/views/VivView.js b/src/views/VivView.js
index 1245930..6dda388 100644
--- a/src/views/VivView.js
+++ b/src/views/VivView.js
@@ -40,7 +40,7 @@ export default class VivView {
    * @param {object} [args.viewState] incoming ViewState object from deck.gl update.
    * @param {object} [args.oldViewState] old ViewState object from deck.gl.
    * @param {object} [args.currentViewState] current ViewState object in react state.
-   * @returns {object} ViewState for this class (or null by default if the ids do not match).
+   * @returns {?object} ViewState for this class (or null by default if the ids do not match).
    */
   filterViewState({ viewState }) {
     const { id, height, width } = this;
@@ -50,8 +50,8 @@ export default class VivView {
   /**
    * Create a layer for this instance.
    * @param {Object} args
-   * @param {ViewState} args.viewStates ViewStates for all current views.
-   * @param {number} args.props Props for this instance.
+   * @param {Object<string,Object>} args.viewStates ViewStates for all current views.
+   * @param {Object} args.props Props for this instance.
    * @returns {Layer} Instance of a layer.
    */
   // eslint-disable-next-line class-methods-use-this,no-unused-vars

Otherwise, your changes work very well for me without any TypeScript errors. Thanks again.

@ilan-gold
Copy link
Collaborator Author

@andreasg123 Just pushed some changes if you want to give them a whirl. Thanks again!

@andreasg123
Copy link
Contributor

@ilan-gold, it works now without problems for me. However, you introduced an inconsistency. For MultiscaleImageLayer and XRLayer, you have id defined twice for the layer parameters, the first time as optional @property {String=} id and the second time as required @property {String} id.

@ilan-gold ilan-gold merged commit 43d4a6c into master Apr 5, 2021
@ilan-gold ilan-gold deleted the ilan-gold/fix_viv_viewer_type branch April 5, 2021 20:46
ilan-gold added a commit that referenced this pull request May 26, 2021
* Fix Interleaved RGB Image Handling. (#399)

* Fix Interleaved RGB Image Handling.

* Prettier.

* Defaults for photometricInterpretation

* Add test for Interleaved BitmapLayer (#400)

* Add test for BitmapLayer

* Add more stringent requirement for layer.

* Fix slicing bug.

* Fix tests better.

* Small fixes.

* ImageLayer too.

* 0.9.4 (#405)

* Fix type/docs for VivView(er) and Consumers. (#407)

* Fix type/docs for VivViewer and VivView (and upstream users)

* Use special jsdoc -> typescript syntax.

* Small Fixes

* Fix ImageLayer

* Try functional component

* Small fixes.

* Small fixes.

* Fix `ImageLayer` `loader` type. (#410)

* Improve emitted TS types for layers (#413)

* Create `Viv` utility type to refine emitted Layer constructor types.

* Do not export VivProps type.

* Fix Empty Loader Selection Bug. (#416)

* 0.9.5 (#417)

* Fix Bugs in `getChannelStats`. (#419)

* Fix Bugs in `getChannelStats`.

* Use > 0 instead of epsilon value.

* Check all properties in onHover to avoid crashes (#426)

* Bump url-parse from 1.4.7 to 1.5.1 in /avivator (#427)

Bumps [url-parse](https://github.com/unshiftio/url-parse) from 1.4.7 to 1.5.1.
- [Release notes](https://github.com/unshiftio/url-parse/releases)
- [Commits](unshiftio/url-parse@1.4.7...1.5.1)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump hosted-git-info from 2.8.8 to 2.8.9 (#429)

Bumps [hosted-git-info](https://github.com/npm/hosted-git-info) from 2.8.8 to 2.8.9.
- [Release notes](https://github.com/npm/hosted-git-info/releases)
- [Changelog](https://github.com/npm/hosted-git-info/blob/v2.8.9/CHANGELOG.md)
- [Commits](npm/hosted-git-info@v2.8.8...v2.8.9)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump lodash from 4.17.20 to 4.17.21 in /avivator (#431)

Bumps [lodash](https://github.com/lodash/lodash) from 4.17.20 to 4.17.21.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](lodash/lodash@4.17.20...4.17.21)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump postcss from 8.1.4 to 8.2.15 in /avivator (#432)

Bumps [postcss](https://github.com/postcss/postcss) from 8.1.4 to 8.2.15.
- [Release notes](https://github.com/postcss/postcss/releases)
- [Changelog](https://github.com/postcss/postcss/blob/main/CHANGELOG.md)
- [Commits](postcss/postcss@8.1.4...8.2.15)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump lodash from 4.17.20 to 4.17.21 (#428)

Bumps [lodash](https://github.com/lodash/lodash) from 4.17.20 to 4.17.21.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](lodash/lodash@4.17.20...4.17.21)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Check for dependabot before running CHANGELOG checks (#433)

* New README Screenshots (#435)

* Add Screenshots for README

* Change width.

* Update URLs

* Allow for LINEAR/NEAREST Filtering Interpolation Choice (#437)

* Allow for LINEAR/NEAREST Filtering Interpolation Choice

* Change variable name

* Change filtering options.

* Bump browserslist from 4.16.1 to 4.16.6 (#441)

Bumps [browserslist](https://github.com/browserslist/browserslist) from 4.16.1 to 4.16.6.
- [Release notes](https://github.com/browserslist/browserslist/releases)
- [Changelog](https://github.com/browserslist/browserslist/blob/main/CHANGELOG.md)
- [Commits](browserslist/browserslist@4.16.1...4.16.6)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump browserslist from 4.14.6 to 4.16.6 in /avivator (#442)

Bumps [browserslist](https://github.com/browserslist/browserslist) from 4.14.6 to 4.16.6.
- [Release notes](https://github.com/browserslist/browserslist/releases)
- [Changelog](https://github.com/browserslist/browserslist/blob/main/CHANGELOG.md)
- [Commits](browserslist/browserslist@4.14.6...4.16.6)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

Co-authored-by: Trevor Manz <trevor.j.manz@gmail.com>
Co-authored-by: Andreas Girgensohn <andreasg123@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
ilan-gold added a commit that referenced this pull request May 26, 2021
* Fix Interleaved RGB Image Handling. (#399)

* Fix Interleaved RGB Image Handling.

* Prettier.

* Defaults for photometricInterpretation

* Add test for Interleaved BitmapLayer (#400)

* Add test for BitmapLayer

* Add more stringent requirement for layer.

* Fix slicing bug.

* Fix tests better.

* Small fixes.

* ImageLayer too.

* 0.9.4 (#405)

* Fix type/docs for VivView(er) and Consumers. (#407)

* Fix type/docs for VivViewer and VivView (and upstream users)

* Use special jsdoc -> typescript syntax.

* Small Fixes

* Fix ImageLayer

* Try functional component

* Small fixes.

* Small fixes.

* Fix `ImageLayer` `loader` type. (#410)

* Improve emitted TS types for layers (#413)

* Create `Viv` utility type to refine emitted Layer constructor types.

* Do not export VivProps type.

* Fix Empty Loader Selection Bug. (#416)

* 0.9.5 (#417)

* Fix Bugs in `getChannelStats`. (#419)

* Fix Bugs in `getChannelStats`.

* Use > 0 instead of epsilon value.

* Check all properties in onHover to avoid crashes (#426)

* Bump url-parse from 1.4.7 to 1.5.1 in /avivator (#427)

Bumps [url-parse](https://github.com/unshiftio/url-parse) from 1.4.7 to 1.5.1.
- [Release notes](https://github.com/unshiftio/url-parse/releases)
- [Commits](unshiftio/url-parse@1.4.7...1.5.1)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump hosted-git-info from 2.8.8 to 2.8.9 (#429)

Bumps [hosted-git-info](https://github.com/npm/hosted-git-info) from 2.8.8 to 2.8.9.
- [Release notes](https://github.com/npm/hosted-git-info/releases)
- [Changelog](https://github.com/npm/hosted-git-info/blob/v2.8.9/CHANGELOG.md)
- [Commits](npm/hosted-git-info@v2.8.8...v2.8.9)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump lodash from 4.17.20 to 4.17.21 in /avivator (#431)

Bumps [lodash](https://github.com/lodash/lodash) from 4.17.20 to 4.17.21.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](lodash/lodash@4.17.20...4.17.21)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump postcss from 8.1.4 to 8.2.15 in /avivator (#432)

Bumps [postcss](https://github.com/postcss/postcss) from 8.1.4 to 8.2.15.
- [Release notes](https://github.com/postcss/postcss/releases)
- [Changelog](https://github.com/postcss/postcss/blob/main/CHANGELOG.md)
- [Commits](postcss/postcss@8.1.4...8.2.15)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump lodash from 4.17.20 to 4.17.21 (#428)

Bumps [lodash](https://github.com/lodash/lodash) from 4.17.20 to 4.17.21.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](lodash/lodash@4.17.20...4.17.21)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Check for dependabot before running CHANGELOG checks (#433)

* New README Screenshots (#435)

* Add Screenshots for README

* Change width.

* Update URLs

* Allow for LINEAR/NEAREST Filtering Interpolation Choice (#437)

* Allow for LINEAR/NEAREST Filtering Interpolation Choice

* Change variable name

* Change filtering options.

* Bump browserslist from 4.16.1 to 4.16.6 (#441)

Bumps [browserslist](https://github.com/browserslist/browserslist) from 4.16.1 to 4.16.6.
- [Release notes](https://github.com/browserslist/browserslist/releases)
- [Changelog](https://github.com/browserslist/browserslist/blob/main/CHANGELOG.md)
- [Commits](browserslist/browserslist@4.16.1...4.16.6)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump browserslist from 4.14.6 to 4.16.6 in /avivator (#442)

Bumps [browserslist](https://github.com/browserslist/browserslist) from 4.14.6 to 4.16.6.
- [Release notes](https://github.com/browserslist/browserslist/releases)
- [Changelog](https://github.com/browserslist/browserslist/blob/main/CHANGELOG.md)
- [Commits](browserslist/browserslist@4.14.6...4.16.6)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

Co-authored-by: Trevor Manz <trevor.j.manz@gmail.com>
Co-authored-by: Andreas Girgensohn <andreasg123@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

Viv 0.93 introduced a TypeScript issue with VivViewer
2 participants