question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Check generic types when using `ViewChild`/`ContentChild` and through DI

See original GitHub issue

Which @angular/* package(s) are relevant/releated to the feature request?

compiler

Description

Imagine we have a parent component NgxOptions<T> and a child NgxOption<T>, with the following definition:

@Component({ /*...*/ })
export class NgxOptions<T> {
  @Input() options: T[];
}

@Component({ /*...*/ })
export class NgxOption<T> {
  @Input() display: (option: T) => string;

  // Forced parent component to be of the same generic type
  constructor(options: NgxOptions<T>) { }
}
<ngx-options [options]="options">
   <!-- I'd expect the compiler and language service to enforce both components have the same generic type -->
   <ngx-option [display]="displayOption"></ngx-option>
</ngx-options>

Proposed solution

It would be great to enforce type checking of components with a generic type when there is a relationship between them, such as ViewChild/ContentChild or injection

Alternatives considered

Right now the strict checking only applied to templates. However, it is only implemented for inputs/outputs, not for generic types in components when they are related.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:13
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
alxhubcommented, Feb 1, 2022

This has always been on the list for template type-checking.

1reaction
Harpushcommented, Dec 30, 2021

This will be awesome! Correct me if I am wrong but that will allow typing context variables on content child template too. A very much needed feature

Read more comments on GitHub >

github_iconTop Results From Across the Web

ViewChildren & ContentChildren • Angular - codecraft.tv
We can solve that by using the alternative @ViewChildren decorator along side the QueryList generic type. TypeScript. Copy import { ViewChildren, QueryList } ......
Read more >
Static query migration guide - Angular
In preparation for this change, in version 8, we are migrating all applications and libraries to explicitly specify the resolution strategy for @ViewChild...
Read more >
changing ViewChild implementation to support multiple ...
I wish to refactor this to work for a list of generic components. I changed my HTML to: <div *ngFor="let row of Page.Rows"...
Read more >
Here is what you need to know about dynamic components in ...
We start with the comparison of dynamic components functionality in Angular and AngularJS and then continue with main concepts that enable dynamic ...
Read more >
Navigate the component tree with DI - Angular
Check out Query , QueryList , ViewChildren , and ContentChildren in the API ... You use standard class injection to acquire a parent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found