Skip to content

bug(cdk): coerceArray doesn't work with readonly arrays #18806

@klemenoslaj

Description

@klemenoslaj

Reproduction

Use StackBlitz to reproduce your issue: https://stackblitz.com/fork/components-issue

Steps to reproduce:

  1. Open: https://stackblitz.com/edit/components-issue-axn4qw?embed=1&file=src/app/example-component.ts&view=editor
  2. TS compile error appears

Expected Behavior

What behavior were you expecting to see?

coerceArray should preserve the readonly array.

Actual Behavior

What behavior did you actually see?
An error:

Argument of type 'readonly number[]' is not assignable to parameter of type 'number | number[]'.
  The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.

Environment

dependencies from stackblitz

  • Angular: 9.0.2
  • CDK/Material: 9.1.0

My thoughts

Type definition is:

export declare function coerceArray<T>(value: T | T[]): T[];

I think it should b:

export declare function coerceArray<T>(value: T | readonly T[]): T[];

This way it would accept both mutable and readonly arrays.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions