Skip to content

Array.isArray works bad with ReadonlyArray #43247

Closed
@awerlogus

Description

@awerlogus

TypeScript Version: 4.3.0-dev.20210315

Search Terms: array isArray readonly

The first problem is that Array.isArray lets us violate readonly modifier and push values to ReadonlyArray:

declare const data: ReadonlyArray<any>

if (Array.isArray(data)) {
  // Expected: Error
  data.push(123)
}

The second problem is that Array.isArray doesn't filter out ReadonlyArray type:

declare const data: Record<string, any> | ReadonlyArray<any>

if (!Array.isArray(data)) {
  // now: readonly any[] | Record<string, any>
  // expected: Record<string, any>
  const alias = data
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fix AvailableA PR has been opened for this issueWorking as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions