Closed
Description
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
}