EightfoldAI / octuple

The Octuple component library
MIT License
17 stars 51 forks source link

feat: typescript improvements #863

Closed gclark-eightfold closed 2 months ago

gclark-eightfold commented 2 months ago

SUMMARY:

  1. Modifies the tsconfig.json to enable JSDoc comments to be compiled with the compiled .d.ts files. This is a DX improvement allowing developers to reference the types instead of needing to look at source code or documentation when writing components.
    
    // lib/components/Dropdown/Dropdown.types.d.ts

// BEFORE: ❌ No JSDoc comments are emitted to the .d.ts files, with export interface DropdownProps { ariaRef?: React.MutableRefObject; children?: React.ReactNode; // ... }

// After: ✅ JSDoc comments are emitted to the .d.ts files export interface DropdownProps { /**

  1. Upgrades @types/react to the latest v17 patch version. This includes a fix for the PropsWithoutRef type which was affecting the compiled type for Panel. See: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/65639
    
    // lib/components/Panel.d.ts

// BEFORE: ❌ This type is VERY long and breaks module augmentation if extending something like HTMLAttributes in application code export declare const Panel: React.ForwardRefExoticComponent<Pick<PanelProps, "placeholder" | "onPointerEnterCapture" | "onPointerLeaveCapture" | "classNames" | "data-test-id" | "data-testid" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | "suppressHydrationWarning" | "accessKey" | "className" | "contentEditable" | "contextMenu" | "dir" | "draggable" | "hidden" | "id" | "lang" | "slot" | "spellCheck" | "style" | "tabIndex" | "title" | "translate" | "radioGroup" | "role" | "about" | "datatype" | "inlist" | "prefix" | "property" | "resource" | "typeof" | "vocab" | "autoCapitalize" | "autoCorrect" | "autoSave" | "color" | "itemProp" | "itemScope" | "itemType" | "itemID" | "itemRef" | "results" | "security" | "unselectable" | "inputMode" | "is" | "aria-activedescendant" | "aria-atomic" | "aria-autocomplete" | "aria-busy" | "aria-checked" | "aria-colcount" | "aria-colindex" | "aria-colspan" | "aria-controls" | "aria-current" | "aria-describedby" | "aria-details" | "aria-disabled" | "aria-dropeffect" | "aria-errormessage" | "aria-expanded" | "aria-flowto" | "aria-grabbed" | "aria-haspopup" | "aria-hidden" | "aria-invalid" | "aria-keyshortcuts" | "aria-label" | "aria-labelledby" | "aria-level" | "aria-live" | "aria-modal" | "aria-multiline" | "aria-multiselectable" | "aria-orientation" | "aria-owns" | "aria-placeholder" | "aria-posinset" | "aria-pressed" | "aria-readonly" | "aria-relevant" | "aria-required" | "aria-roledescription" | "aria-rowcount" | "aria-rowindex" | "aria-rowspan" | "aria-selected" | "aria-setsize" | "aria-sort" | "aria-valuemax" | "aria-valuemin" | "aria-valuenow" | "aria-valuetext" | "children" | "dangerouslySetInnerHTML" | "onCopy" | "onCopyCapture" | "onCut" | "onCutCapture" | "onPaste" | "onPasteCapture" | "onCompositionEnd" | "onCompositionEndCapture" | "onCompositionStart" | "onCompositionStartCapture" | "onCompositionUpdate" | "onCompositionUpdateCapture" | "onFocus" | "onFocusCapture" | "onBlur" | "onBlurCapture" | "onChange" | "onChangeCapture" | "onBeforeInput" | "onBeforeInputCapture" | "onInput" | "onInputCapture" | "onReset" | "onResetCapture" | "onSubmit" | "onSubmitCapture" | "onInvalid" | "onInvalidCapture" | "onLoad" | "onLoadCapture" | "onError" | "onErrorCapture" | "onKeyDown" | "onKeyDownCapture" | "onKeyPress" | "onKeyPressCapture" | "onKeyUp" | "onKeyUpCapture" | "onAbort" | "onAbortCapture" | "onCanPlay" | "onCanPlayCapture" | "onCanPlayThrough" | "onCanPlayThroughCapture" | "onDurationChange" | "onDurationChangeCapture" | "onEmptied" | "onEmptiedCapture" | "onEncrypted" | "onEncryptedCapture" | "onEnded" | "onEndedCapture" | "onLoadedData" | "onLoadedDataCapture" | "onLoadedMetadata" | "onLoadedMetadataCapture" | "onLoadStart" | "onLoadStartCapture" | "onPause" | "onPauseCapture" | "onPlay" | "onPlayCapture" | "onPlaying" | "onPlayingCapture" | "onProgress" | "onProgressCapture" | "onRateChange" | "onRateChangeCapture" | "onSeeked" | "onSeekedCapture" | "onSeeking" | "onSeekingCapture" | "onStalled" | "onStalledCapture" | "onSuspend" | "onSuspendCapture" | "onTimeUpdate" | "onTimeUpdateCapture" | "onVolumeChange" | "onVolumeChangeCapture" | "onWaiting" | "onWaitingCapture" | "onAuxClick" | "onAuxClickCapture" | "onClick" | "onClickCapture" | "onContextMenu" | "onContextMenuCapture" | "onDoubleClick" | "onDoubleClickCapture" | "onDrag" | "onDragCapture" | "onDragEnd" | "onDragEndCapture" | "onDragEnter" | "onDragEnterCapture" | "onDragExit" | "onDragExitCapture" | "onDragLeave" | "onDragLeaveCapture" | "onDragOver" | "onDragOverCapture" | "onDragStart" | "onDragStartCapture" | "onDrop" | "onDropCapture" | "onMouseDown" | "onMouseDownCapture" | "onMouseEnter" | "onMouseLeave" | "onMouseMove" | "onMouseMoveCapture" | "onMouseOut" | "onMouseOutCapture" | "onMouseOver" | "onMouseOverCapture" | "onMouseUp" | "onMouseUpCapture" | "onSelect" | "onSelectCapture" | "onTouchCancel" | "onTouchCancelCapture" | "onTouchEnd" | "onTouchEndCapture" | "onTouchMove" | "onTouchMoveCapture" | "onTouchStart" | "onTouchStartCapture" | "onPointerDown" | "onPointerDownCapture" | "onPointerMove" | "onPointerMoveCapture" | "onPointerUp" | "onPointerUpCapture" | "onPointerCancel" | "onPointerCancelCapture" | "onPointerEnter" | "onPointerLeave" | "onPointerOver" | "onPointerOverCapture" | "onPointerOut" | "onPointerOutCapture" | "onGotPointerCapture" | "onGotPointerCaptureCapture" | "onLostPointerCapture" | "onLostPointerCaptureCapture" | "onScroll" | "onScrollCapture" | "onWheel" | "onWheelCapture" | "onAnimationStart" | "onAnimationStartCapture" | "onAnimationEnd" | "onAnimationEndCapture" | "onAnimationIteration" | "onAnimationIterationCapture" | "onTransitionEnd" | "onTransitionEndCapture" | "push" | "locale" | "size" | "autoFocus" | "configContextProps" | "footer" | "actionButtonOneProps" | "actionButtonTwoProps" | "actionButtonThreeProps" | "bodyClassNames" | "bodyPadding" | "closeButtonAriaLabelText" | "closeButtonProps" | "closable" | "closeIcon" | "firstFocusableSelector" | "focusTrap" | "footerClassNames" | "gradient" | "headerButtonProps" | "headerClassNames" | "headerIcon" | "headerPadding" | "height" | "lastFocusableSelector" | "maskClosable" | "onClose" | "onVisibleChange" | "overlay" | "parent" | "panelClassNames" | "panelHeader" | "panelStyle" | "panelWrapperClassNames" | "placement" | "renderContentAlways" | "scrollLock" | "skipFocusableSelectorsFromIndex" | "theme" | "themeContainerId" | "visible" | "width" | "zIndex"> & React.RefAttributes>;

// AFTER: ✅ This type is much better and works withmodule augmentation if extending something like HTMLAttributes in application code export declare const Panel: React.ForwardRefExoticComponent<Omit<PanelProps, "ref"> & React.RefAttributes>;

```ts
// my-app/types/react.d.ts (in application / consuming code)

// BEFORE: ❌ Augmenting HTMLAttributes works for all Octuple components *except* for Panel.
// AFTER:  ✅ Augmenting HTMLAttributes works for ALL Octuple components.
import { HTMLAttributes } from "react";
declare module "react" {
  interface HTMLAttributes<T> {
    /**
     * An example global attribute w/ expected types defined.
     */
    "data-custom-global-attribute"?: "open" | "closed";
  }
}

GITHUB ISSUE (Open Source Contributors)

JIRA TASK (Eightfold Employees Only):

https://eightfoldai.atlassian.net/browse/ENG-100260

CHANGE TYPE:

TEST COVERAGE:

TEST PLAN:

  1. Run yarn build
    • [ ] Observe that the files like lib/components/Dropdown/Dropdown.types.d.ts include JSDoc comments.
  2. Create a pre-release build of Octuple and install in application code (or patch the node_modules locally)
  3. Include the above snippet somewhere in application code to augment HTMLAttributes for react.
    • [ ] When typing data- on a Panel component, you will now see an autocomplete suggestion for data-custom-global-attribute.
    • [ ] Passing an invalid prop to data-custom-global-attribute on Panel will show a type error.
codesandbox-ci[bot] commented 2 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 84.63%. Comparing base (6de011a) to head (492f9af).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #863 +/- ## ======================================= Coverage 84.63% 84.63% ======================================= Files 1098 1098 Lines 20354 20354 Branches 7690 7690 ======================================= Hits 17226 17226 Misses 3043 3043 Partials 85 85 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.