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(types): type fixes #1638

Merged
merged 5 commits into from
Dec 12, 2024
Merged

Conversation

jmannau
Copy link
Contributor

@jmannau jmannau commented Nov 29, 2024

Context

The release includes imports to internal files in the generated typescript types. This means there are typescript errors if one of the affected packages is imported into other project, the typescript compilation fails.

For example, the compiled Typescript file cornerstone3D/packages/tools/dist/esm/stateManagement/segmentation/helpers/getSegmentationActor.d.ts

is

export declare function getLabelmapActorUID(viewportId: string, segmentationId: string): string | undefined;
export declare function getLabelmapActorEntry(viewportId: string, segmentationId: string): import("packages/core/dist/esm/types").ActorEntry;
export declare function getSurfaceActorEntry(viewportId: string, segmentationId: string, segmentIndex?: number | string): import("packages/core/dist/esm/types").ActorEntry;
export declare function getSurfaceActorUID(viewportId: string, segmentationId: string, segmentIndex?: number | string): string | undefined;
export declare function getSurfaceRepresentationUID(segmentationId: string, segmentIndex?: number | string): string;

which includes export declare function getLabelmapActorEntry(viewportId: string, segmentationId: string): import("packages/core/dist/esm/types").ActorEntry; and export declare function getSurfaceActorEntry(viewportId: string, segmentationId: string, segmentIndex?: number | string): import("packages/core/dist/esm/types").ActorEntry;

These imports are invalid in the distributed module.

To verify:

  1. yarn build
  2. grep -r 'esm/' packages/**/*.d.ts
  3. The output lists the files with incorrect imports

Changes & Results

In order to prevent the incorrect generate type

Before

cornerstone3D % grep -r 'esm/' packages/**/*.d.ts
packages/dicomImageLoader/dist/esm/imageLoader/internal/streamRequest.d.ts:export default function streamRequest(url: string, imageId: string, defaultHeaders?: Record<string, string>, options?: CornerstoneWadoRsLoaderOptions): import("packages/core/dist/esm/utilities/ProgressiveIterator").PromiseIterator<unknown>;
packages/dicomImageLoader/dist/esm/imageLoader/wadors/getPixelData.d.ts:declare function getPixelData(uri: string, imageId: string, mediaType?: string, options?: CornerstoneWadoRsLoaderOptions): import("packages/core/dist/esm/utilities/ProgressiveIterator").PromiseIterator<unknown> | import("../../types").LoaderXhrRequestPromise<{
packages/dicomImageLoader/dist/esm/imageLoader/wadors/getPixelData.d.ts:    imageQualityStatus: import("packages/core/dist/esm/enums").ImageQualityStatus;
packages/dicomImageLoader/dist/esm/imageLoader/wadors/getPixelData.d.ts:    imageQualityStatus: import("packages/core/dist/esm/enums").ImageQualityStatus;
packages/tools/dist/esm/stateManagement/segmentation/helpers/getSegmentationActor.d.ts:export declare function getLabelmapActorEntry(viewportId: string, segmentationId: string): import("packages/core/dist/esm/types").ActorEntry;
packages/tools/dist/esm/stateManagement/segmentation/helpers/getSegmentationActor.d.ts:export declare function getSurfaceActorEntry(viewportId: string, segmentationId: string, segmentIndex?: number | string): import("packages/core/dist/esm/types").ActorEntry;
packages/tools/dist/esm/utilities/voi/windowlevel/extractWindowLevelRegionToolData.d.ts:    scalarData: import("packages/core/dist/esm/types").PixelDataTypedArray;

After

cornerstone3D % grep -r 'esm/' packages/**/*.d.ts
packages/dicomImageLoader/dist/esm/imageLoader/internal/streamRequest.d.ts:export default function streamRequest(url: string, imageId: string, defaultHeaders?: Record<string, string>, options?: CornerstoneWadoRsLoaderOptions): import("packages/core/dist/esm/utilities/ProgressiveIterator").PromiseIterator<unknown>;
packages/dicomImageLoader/dist/esm/imageLoader/wadors/getPixelData.d.ts:declare function getPixelData(uri: string, imageId: string, mediaType?: string, options?: CornerstoneWadoRsLoaderOptions): import("packages/core/dist/esm/utilities/ProgressiveIterator").PromiseIterator<unknown> | import("../../types").LoaderXhrRequestPromise<{
packages/dicomImageLoader/dist/esm/imageLoader/wadors/getPixelData.d.ts:    imageQualityStatus: import("packages/core/dist/esm/enums").ImageQualityStatus;
packages/dicomImageLoader/dist/esm/imageLoader/wadors/getPixelData.d.ts:    imageQualityStatus: import("packages/core/dist/esm/enums").ImageQualityStatus;

This patch fixes @cornerstonejs/tools.

However incorrect imports remain in @cornerstonejs/dicomImageLoader. I wasn't able to determine the correct types as some appear not to be publicly exposed currently.

Testing

  1. check out branch
  2. yarn build
  3. grep -r 'esm/' packages/tools/**/*.d.ts will find no incorrect imports in *.d.ts files

Checklist

PR

  • My Pull Request title is descriptive, accurate and follows the
    semantic-release format and guidelines.

Code

Public Documentation Updates

  • [NA] The documentation page has been updated as necessary for any public API
    additions or removals.

Tested Environment

  • "OS: MacOS 15.1.1"
  • "Node version: 20.11.1"

Prevent invalid internal type import in the generated .d.ts types file
Prevent internal import in generated .d.ts types
Copy link

stackblitz bot commented Nov 29, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

netlify bot commented Nov 29, 2024

Deploy Preview for cornerstone-3d-docs ready!

Name Link
🔨 Latest commit db1e0ea
🔍 Latest deploy log https://app.netlify.com/sites/cornerstone-3d-docs/deploys/675a2eaca2fa51000823c3a5
😎 Deploy Preview https://deploy-preview-1638--cornerstone-3d-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

} from '@cornerstonejs/core';

function extractWindowLevelRegionToolData(viewport) {
function extractWindowLevelRegionToolData(
viewport: VolumeViewport | StackViewport
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a type, IVolumeViewport | IStackViewport i guess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it can be. I thought the stricter type of VolumeViewport | StackViewport was more appropriate because the function itself throws an error if viewport is not an instance of VolumeViewport or StackViewport. If the argument type accepted IVolumeViewport | IStackViewport it would be theoretically possible to pass something else, causing an error to be thrown. Hence the stricter type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, just checking if you have a preference between the stricter viewport: VolumeViewport | StackViewport type or viewport: IVolumeViewport | IStackViewport. I can update the PR to suit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember a lot of issues with using classes as types with API Extractor in the past. It was not smart enough to use types for them, and I think the rest of the app uses types, which, in fact, are the same. If you look inside IVolumeViewport, it says:

import type { VolumeViewport } from '../RenderingEngine';

type IVolumeViewport = VolumeViewport;

export type { IVolumeViewport as default };

So I think I prefer IVolumeViewport.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problems. Updated.
James

@sedghi sedghi changed the title Feature/type fixes fix(types): type fixes Dec 12, 2024
@sedghi sedghi merged commit 87626a5 into cornerstonejs:main Dec 12, 2024
26 checks passed
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.

2 participants