-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Open
Labels
Experimentation NeededSomeone needs to try this out to see what happensSomeone needs to try this out to see what happensSuggestionAn idea for TypeScriptAn idea for TypeScript
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 : falsei 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
ArrayandReadonlyArray, resulting in duplicate code Array<unknown> extends ReadonlyArray<unknown> ? true : falseresolves totrueeven after adding members toReadonlyArraythatArraydoesn't have
🙂 Expected behavior
Array should explicitly extend ReadonlyArray
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Experimentation NeededSomeone needs to try this out to see what happensSomeone needs to try this out to see what happensSuggestionAn idea for TypeScriptAn idea for TypeScript