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

feat(tree): typescript/strong typing of nodes #30502

Open
aceArt-GmbH opened this issue Feb 17, 2025 · 2 comments
Open

feat(tree): typescript/strong typing of nodes #30502

aceArt-GmbH opened this issue Feb 17, 2025 · 2 comments
Labels
area: cdk/tree feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@aceArt-GmbH
Copy link

aceArt-GmbH commented Feb 17, 2025

Feature Description

Currently *matTreeNodeDef types the resulting variable as any:

Image

Use Case

enable you to build web apps with confidence!

The lack of strong typing is really problematic for bigger projects, and it feels like a downgrade in developer experience compared to custom build trees.

Further motivation for this has been described in:

#16273
angular/angular#28731
https://nartc.me/blog/typed-mat-cell-def/
#22290

Workaround copied from #22290:

import { Directive, input } from '@angular/core';
import { MatTree, MatTreeNodeDef } from '@angular/material/tree';

@Directive({
   selector: '[matTreeNodeDef]',
   providers: [
      { provide: MatTreeNodeDef, useExisting: TypeSafeMatTreeNodeDefDirective },
   ],
})
export class TypeSafeMatTreeNodeDefDirective<T> extends MatTreeNodeDef<
   NoInfer<T>
> {
   readonly matTreeNodeDefTree = input.required<MatTree<T>>();

   static ngTemplateContextGuard<T>(
      dir: TypeSafeMatTreeNodeDefDirective<T>,
      ctx: any,
   ): ctx is { $implicit: T; index: number } {
      return true;
   }
}
-<mat-tree [dataSource]="dataSource" [childrenAccessor]="childrenAccessor">
+<mat-tree #tree [dataSource]="dataSource" [childrenAccessor]="childrenAccessor">
...
-<mat-tree-node *matTreeNodeDef="let node;" matTreeNodePadding>
+<mat-tree-node *matTreeNodeDef="let node; tree: tree" matTreeNodePadding>
@aceArt-GmbH aceArt-GmbH added feature This issue represents a new feature or feature request rather than a bug or bug fix needs triage This issue needs to be triaged by the team labels Feb 17, 2025
@crisbeto crisbeto added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent area: cdk/tree and removed needs triage This issue needs to be triaged by the team labels Feb 18, 2025
@crisbeto
Copy link
Member

Worth noting that this will be a breaking change.

@aceArt-GmbH
Copy link
Author

it may break builds, but only if they were using wrong types.
A migration could insert $any() at all places (which would error)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: cdk/tree feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests

2 participants