Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array should extend ReadonlyArray #46394

Open
DetachHead opened this issue Oct 16, 2021 · 4 comments · May be fixed by #46781
Open

Array should extend ReadonlyArray #46394

DetachHead opened this issue Oct 16, 2021 · 4 comments · May be fixed by #46781
Labels
Experimentation Needed Someone needs to try this out to see what happens Suggestion An idea for TypeScript

Comments

@DetachHead
Copy link
Contributor

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

@DetachHead DetachHead changed the title array should extend ReadonlyArray Array should extend ReadonlyArray Oct 16, 2021
@andrewbranch andrewbranch added Experimentation Needed Someone needs to try this out to see what happens Suggestion An idea for TypeScript labels Nov 10, 2021
@andrewbranch
Copy link
Member

I think the best way forward is to open a draft PR so we can run all the extended tests on it and see if it breaks any projects in the wild.

@DetachHead
Copy link
Contributor Author

Alright, I'll give it a go

@DetachHead
Copy link
Contributor Author

#46781

@sandersn
Copy link
Member

This is related to #36554 and should become part of that at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experimentation Needed Someone needs to try this out to see what happens Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants