Skip to content

ngTemplateOutletContext not checked strictly in template #43510

@moniuch

Description

@moniuch

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

I have a generic component, ListComponent which I allow user to template using structural directives, like so:

<app-list>
  <div *appListFooter="let count = count">Total {{ count }} items</div>
</app-list>

FooterDirective:

@Directive({
  selector: '[appListFooter]',
})
export class ListFooterDirective {
  constructor(
    public tpl: TemplateRef<any>,
  ) {}
}

ListComponent:

@Component({
  selector: 'app-list',
  template: `
    <ng-container *ngIf="footer"
                  [ngTemplateOutlet]="footer.tpl"
                  [ngTemplateOutletContext]="{count: count}"
    ></ng-container>
  `,
})
export class ListComponent {
  @ContentChild(ListFooterDirective) footer: ListFooterDirective;
}

When trying to strongly type TemplateRef context:

export interface FooterContext {
  count: number;
}

public tpl: TemplateRef<FooterContext>,

I noticed that the contract is ignored and I can type anything in the interface without any complains from the compiler.

export interface FooterContext {
  foo: string;
}

Could be that the typing is only used in case of vcRef.createEmbeddedView(this.tpl, { /* context */ }) which I don't seem to need in this case.

Given that strict template checking was recently improved in Angular, I assume by now there are tools in place for context typing to be checked on a template level, it just needs to be implemented.

Or please correct me if I misunderstand the concept here.
This is terribly needed when authoring components for others to consume.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in

No response

Anything else?

BTW, the bug report template requires an answer whether the bug is regression or not. I think "I don't know" would be quite a legit answer from those who are not 100% sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: commonIssues related to APIs in the @angular/common packagecompiler: template type-checking

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions