Skip to content

Conversation

@metonym
Copy link
Collaborator

@metonym metonym commented Nov 29, 2025

Related #2316, #2315, #2271, #1954

This enhances several components to support TypeScript generics, providing an improved developer experience. This is especially useful for cases where objects are passed as props and may be passed back to the consumer.

The implementation follows the existing pattern used in ComboBox, DataTable, and SelectableTileGroup, allowing consumers to specify custom types while maintaining full backward compatibility through default type parameters.

Changes should be backwards-compatible.

Changes

  • TreeView: add generic Node type parameter extending TreeNode.
  • RecursiveList: add generic Node type parameter extending RecursiveListNode. Typedef uses RecursiveListNode[] to avoid DOM Node conflicts.
  • HeaderSearch: add generic Result type parameter extending HeaderSearchResult.
  • Select: add generic Value type parameter extending string | number.

Custom type for TreeView nodes

<script lang="ts">
  type FileNode = {
    id: string;
    text: string;
    fileSize?: number;
    nodes?: FileNode[];
  };
  
  const nodes: FileNode[] = [
    { id: "1", text: "Documents", nodes: [
      { id: "2", text: "file.txt", fileSize: 1024 }
    ]}
  ];
</script>
<TreeView {nodes} on:select={(e) => {
  // e.detail is FileNode & { expanded, leaf, selected }
}} />

Screenshot 2025-11-29 at 11 10 29 AM

Enum for Select

<script lang="ts">
  type Status = "pending" | "approved" | "rejected";
  let selected: Status | undefined;
</script>

<Select bind:selected on:update={(e) => {
  // e.detail is Status (not just string | number)
}} />
Screenshot 2025-11-29 at 11 11 48 AM

@metonym metonym force-pushed the metonym/more-generics branch from 2b87749 to 48e8f09 Compare November 29, 2025 19:12
@metonym metonym merged commit d57b375 into master Nov 29, 2025
7 checks passed
@metonym metonym deleted the metonym/more-generics branch November 29, 2025 19:15
@metonym
Copy link
Collaborator Author

metonym commented Nov 29, 2025

Released in v0.94.0

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.

1 participant