Open
Description
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
andReadonlyArray
, resulting in duplicate code Array<unknown> extends ReadonlyArray<unknown> ? true : false
resolves totrue
even after adding members toReadonlyArray
thatArray
doesn't have
🙂 Expected behavior
Array
should explicitly extend ReadonlyArray