Skip to content

Strictly-typed option value for typed select, radio and checkbox inputs #59923

Open

Description

@martinboue

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

forms

Description

Option values for selectable input (select, radio or checkbox) should be type checked when using typed form control.

Proposed solution

value attribute should have the same type as its form control value.

If type does not match, we would have a Typescript error.

Example

Let's say I have a form control with the following type:

country: FormControl<string>

and the following template:

<select [formControl]="country">
  <!-- Valid type -->
  <option value="France">
  <!-- Incorrect types -->
  <option [value]="0"> 
  <option [value]="true">
</select>

I want option 0and true to fail to compile, because they do not match the type string.

Same principle would apply to <input type="radio" /> and <input type="checkbox" />.

Alternatives considered

If not doable, could be specific to Angular Material components.

Activity

  1. added
    area: compiler
    on Feb 12, 2025
  2. added this to the needsTriage milestone on Feb 12, 2025
  3. JeanMeche commented on Feb 12, 2025

    @JeanMeche
    Member

    Today, this is due to typechecking not being enabled on HTML elements.

    // Even in full template type-checking mode, DOM binding checks are not quite ready yet.
    checkTypeOfDomBindings: false,
    checkTypeOfOutputEvents: strictTemplates,

    Flipping that boolean is easy but it will be breaking.

  4. self-assigned this
    on Feb 12, 2025
  5. JoostK commented on Feb 12, 2025

    @JoostK
    Member

    @devversion has a PR in #54098 which is a bit more involved than flipping the flag to improve the ergonomics, as it seems that DOM declarations aren't as forgiving as typically used in Angular templates.

  6. Satish-Medar commented on Feb 13, 2025

    @Satish-Medar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: compilercompiler: template type-checking

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions