Skip to content

fix: improve type safety for TreeView and TreeViewItem APIs#505

Merged
willeastcott merged 2 commits into
mainfrom
fix/treeview-type-safety
Feb 26, 2026
Merged

fix: improve type safety for TreeView and TreeViewItem APIs#505
willeastcott merged 2 commits into
mainfrom
fix/treeview-type-safety

Conversation

@willeastcott

@willeastcott willeastcott commented Feb 26, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replaces onReparent: any with a properly typed callback using a new exported ReparentedItem interface
  • Adds import type { TreeView } to TreeViewItem and types the public treeView getter/setter as TreeView | null
  • Adds explicit string | null type to the filter setter
  • Fixes mismatched quote and missing type annotation in JSDoc examples
  • Removes unsafe as TreeViewItem casts in _getChildIndex and the _onReparentFn reparent branch

Details

ReparentedItem interface (new, exported):

  • item: TreeViewItem - the reparented item
  • oldParent: Element - the old parent
  • newParent?: Element - the new parent (set after reparenting)
  • newChildIndex?: number - the new child index

TreeViewItem.treeView getter/setter now typed as TreeView | null instead of any. The internal _treeView field remains any since it calls protected TreeView methods.

_getChildIndex refactored to accept Container instead of TreeViewItem. The old implementation used indexOf(...) - 1 which relied on both TreeView and TreeViewItem having exactly one internal DOM child before the TreeViewItem children. The new implementation counts only TreeViewItem children via the dom.ui back-reference, making it robust regardless of how many internal nodes the parent has.

_onReparentFn branch similarly updated: fakeDom/getChildren widened from TreeViewItem to Container, and the blanket r.newChildIndex-- replaced with a treeItemIndex helper that counts only TreeViewItem nodes.

Test plan

  • Full build passes including type generation (npm run build)
  • All existing tests pass (npm test)
  • Lint passes (npm run lint)

@willeastcott willeastcott force-pushed the fix/treeview-type-safety branch from bf691d9 to 4bda843 Compare February 26, 2026 14:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the public TypeScript surface area of TreeView / TreeViewItem by replacing any-typed APIs with explicit, reusable types and correcting JSDoc examples so generated types/docs are more accurate.

Changes:

  • Introduces and exports a ReparentedItem interface and uses it to type the onReparent callback and internal reparent bookkeeping.
  • Types TreeViewItem.treeView as TreeView | null using a type-only import to avoid runtime circular dependencies.
  • Tightens TreeView.filter setter typing and fixes JSDoc example typing/quotes.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/components/index.ts Re-exports ReparentedItem from the main components barrel.
src/components/TreeViewItem/index.ts Adds `TreeView
src/components/TreeView/index.ts Defines ReparentedItem, types onReparent, improves reparent typing, and types filter setter.
Comments suppressed due to low confidence (1)

src/components/TreeView/index.ts:1204

  • filter now accepts string | null, but the getter has no explicit return type and currently returns this._filter (declared as string). This can lead to an inconsistent (and less useful) public declaration type in the emitted .d.ts (getter as string, setter as string | null). Consider updating the backing field and getter to string | null so the accessor pair describes the same public property type.
    set filter(value: string | null) {
        if (this._filter === value) return;

        this._filter = value;

        if (value) {
            this._applyFilter(value);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/components/TreeView/index.ts
Comment thread src/components/TreeView/index.ts Outdated
@willeastcott willeastcott merged commit 2b197aa into main Feb 26, 2026
4 checks passed
@willeastcott willeastcott deleted the fix/treeview-type-safety branch February 26, 2026 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants