Report bindings that have no effect on ng-container, ng-template, and ng-content

未关闭
#71,881 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
20/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
领域
frontend, testing

调研方向

Start by reading the Angular compiler, compiler-cli, and core paths responsible for template type checking, extended diagnostics, language-service completions, and dev-mode binding checks. The proposed work spans diagnostics, runtime logging, parsing, and later breaking severity changes; the issue links PRs #71882, #71883, and #71884, which indicate work is already underway. Done means ignored bindings and invalid component selectors are reported as described, while bindings that take effect remain unreported.

由索引模型根据 Issue 内容生成。

描述

area: compiler
Which @angular/* package(s) are relevant/related to the feature request?

compiler, compiler-cli, core

Description

<ng-container>, <ng-template>, and <ng-content> don't render an element. Angular accepts
bindings and attributes on them that can never take effect, and most of them compile and run
without a word:

<ng-container class="card" [class.active]="isActive" (click)="select()">...</ng-container>
<ng-template [title]="label" (click)="missing()">...</ng-template>
<ng-content selector="h1" />

Nothing on these lines does anything:

  • Class, style, and event bindings on <ng-container> and <ng-template> are ignored, unless a
    directive claims them.
  • Bindings that no directive claims on <ng-template> aren't type-checked, so
    (click)="missing()" compiles.
  • <ng-content> ignores every binding, and every attribute except select and ngProjectAs. The
    selector typo (for select) silently makes the slot a default slot. Angular's own upgrade
    examples have had this typo since 2017.
  • NO_ERRORS_SCHEMA hides unknown properties on <ng-container>, although no schema can give it
    a property.
  • Inside <svg>, [attr.x] on <ng-container> skips its parse error and throws a misleading
    assertion in dev mode.
Proposed solution

Report what Angular ignores, and nothing that takes effect. Code generation and runtime behavior
don't change.

Step 1, not breaking:

  • NG8031 ignoredBinding, a new type-check diagnostic. It needs
    strictTemplates, is a warning by default, and is configured through
    extendedDiagnostics.checks, like NG8011. It reports class, style, attr., animation, and
    event bindings that no directive claims, every binding on <ng-content>, and static attributes
    that nothing reads.
  • NG8032, a template type-check warning for a component whose selector matches
    <ng-container> or <ng-template>. That compiles today and fails only when the template
    renders, in dev and production mode.
  • The language service stops suggesting DOM attributes on <ng-container>.
  • NG0319, a dev-mode runtime log for class, style, and event bindings on <ng-container> and
    <ng-template>, so JIT is covered too.
  • The <svg> parse fix.

Step 2, breaking: severities only, in two independent PRs.

  • NG8032 is an error.
  • NG8031 is an error by default, and NG0319 throws under errorOnUnknownProperties.

Every new error points at a binding or component that never worked. NG8031 can be set back to
"warning" or "suppress".

Not reported: bindings a directive claims, directive host bindings (directives are reusable),
global event targets, and static attributes that a directive, a structural directive, a content
projection slot, or Angular itself reads.

Question for the team: are the step 2 PRs fine for a minor release, or should they wait for a
major? They can land separately.

PRs: #71882 (step 1); #71883 (NG8032 error) and #71884 (NG8031 error), each building on #71882

Alternatives considered
  • A template parse error. The linker re-parses published libraries, so a parse error would
    break libraries that compile today.
  • Only an extended diagnostic. It can warn, but step 2 needs the same check in the type
    checker. One check in the type checker serves both levels.
  • Only a runtime error. It reports late, in dev mode only, and can't see bindings on
    <ng-content> or which static attributes something reads.
  • Also reporting directive host bindings. A directive can apply to elements where those
    bindings work, so reporting them would be noise.
主要语言
TypeScript
星标
101k
派生
28.2k
平均合并
2 天 45 分钟
30 天内合并 PR
373

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

angular/angular 的其他 Issue

查看 angular/angular 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。