Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

SkyZeroZx
Copy link
Contributor

@SkyZeroZx SkyZeroZx commented Sep 10, 2025

Feat: Add Numeric CSS Property Requires Unit Diagnostic

Overview

This PR introduces a new extended template diagnostic to Angular's compiler: NumericCssPropertyRequiresUnit. It warns developers when a numeric value is bound to a CSS property that requires a unit (e.g., [style.width]="500"), which would have no effect in the browser. The diagnostic suggests using a unit suffix (e.g., [style.width.px]="500" or [style.width]="'500px'").

Motivation

Binding numeric values to CSS properties without units is a common source of confusion and bugs. Browsers ignore such values, leading to unexpected UI behavior. This diagnostic helps developers catch these mistakes early and provides actionable suggestions.

Example

<!-- Triggers warning -->
<div [style.width]="500"></div>

<!-- Correct usage -->
<div [style.width.px]="500"></div>
<div [style.width]="'500px'"></div>

Diagnostic Message

Binding a number to the CSS property 'width' will have no effect. CSS requires units for length values. E.g Use '[style.width.px]="500"' or '[style.width]="'500px'"' instead.

Impact

  • Developer Experience: Improves DX by catching subtle template bugs.
  • Backward Compatibility: No breaking changes. Only adds a warning for incorrect usage.
  • Extensibility: Easily extendable to more CSS properties if needed.

[Related Issue]
#61381

@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: compiler Issues related to `ngc`, Angular's template compiler labels Sep 10, 2025
@ngbot ngbot bot added this to the Backlog milestone Sep 10, 2025
add check for numeric CSS properties requiring units
@SkyZeroZx SkyZeroZx force-pushed the add-numberic-css-property-requieres branch from 83bf91c to 651e7c1 Compare September 10, 2025 19:01
@JeanMeche JeanMeche added the state: blocked on G3 cleanup This change requires a G3 cleanup label Sep 11, 2025
@JeanMeche
Copy link
Member

This change will require someone to clean-up G3 to allow us to land this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: compiler Issues related to `ngc`, Angular's template compiler detected: feature PR contains a feature commit state: blocked on G3 cleanup This change requires a G3 cleanup
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants