Skip to content

bug(CdkListbox): cdkListboxValue should allow undefined for initial value #31594

@shhdharmen

Description

@shhdharmen

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

Currently, cdkListboxValue does not allow any other value than what is given as value for cdkOptions. This is fine as it prevents from selecting invalid value.

But, it should also allow undefined so that initial state of listbox works fine without needing to have a selected value.

Reproduction

StackBlitz link: https://stackblitz.com/edit/ofsskkdf?file=src%2Fexample%2Fcdk-listbox-value-binding-example.ts

Observe that Angular Compiler is giving error:

✘ [ERROR] TS2322: Type 'string[] | undefined' is not assignable to type 'readonly string[]'.
  Type 'undefined' is not assignable to type 'readonly string[]'. [plugin angular-compiler]

    src/example/cdk-listbox-value-binding-example.html:7:7:
      7 │       [cdkListboxValue]="starter"
        ╵        ~~~~~~~~~~~~~~~

  Error occurs in the template of component CdkListboxValueBindingExample.

    src/example/cdk-listbox-value-binding-example.ts:9:15:
      9 │   templateUrl: 'cdk-listbox-value-binding-example.html',
        ╵                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


✘ [ERROR] TS4104: The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'. [plugin angular-compiler]

    src/example/cdk-listbox-value-binding-example.html:8:31:
      8 │       (cdkListboxValueChange)="starter = $event.value"
        ╵                                ~~~~~~~~~~~~~~~~~~~~~~

  Error occurs in the template of component CdkListboxValueBindingExample.

    src/example/cdk-listbox-value-binding-example.ts:9:15:
      9 │   templateUrl: 'cdk-listbox-value-binding-example.html',
        ╵                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

cdkListboxValue should allow undefined to handle initial state.

Actual Behavior

cdkListboxValue does not allow any other type of values except what is given as value in its cdkOptions.

Environment

  • Angular: 20.1.0
  • CDK/Material: 20.1.0
  • Browser(s): Chrome
  • Operating System (e.g. Windows, macOS, Ubuntu): macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue needs to be triaged by the team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions