Skip to content

Array should extend ReadonlyArray #46394

Open
@DetachHead

Description

@DetachHead

Bug Report

it's annoying how to extend arrays you have to duplicate your property in two interfaces

declare global {
    interface Array<T> {
      foo(): T
    }
    interface ReadonlyArray<T> {
      foo(): T
    }
}

this seems to be because Array only structurally extends ReadonlyArray, so adding to ReadonlyArray means Array no longer extends it

//true
type ArrayExtendsReadonlyArray = Array<unknown> extends ReadonlyArray<unknown> ? true : false

i believe the fix is to add this to Array

interface Array<T> extends ReadonlyArray<T> {}

🔎 Search Terms

array extends readonlyarray

🕗 Version & Regression Information

4.5.0-dev.20211015

⏯ Playground Link

Playground link with relevant code

💻 Code

interface ReadonlyArray<T> {
  foo(): T
}

//this returns true, but should return false because Array no longer extends ReadonlyArray
type ArrayExtendsReadonlyArray = Array<unknown> extends ReadonlyArray<unknown> ? true : false

declare const array: Array<unknown>

//error: Property 'foo' does not exist on type 'unknown[]'.(2339)
array.foo()

//but uncommenting this line will fix it:
// interface Array<T> extends ReadonlyArray<T> {}

🙁 Actual behavior

  • have to add to both Array and ReadonlyArray, resulting in duplicate code
  • Array<unknown> extends ReadonlyArray<unknown> ? true : false resolves to true even after adding members to ReadonlyArray that Array doesn't have

🙂 Expected behavior

Array should explicitly extend ReadonlyArray

Metadata

Metadata

Assignees

No one assigned

    Labels

    Experimentation NeededSomeone needs to try this out to see what happensSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions