Microsoft/TypeScript

Misleading error message when interface tries to extend constrained type parameter

Open

#57.037 geöffnet am 12. Jan. 2024

Auf GitHub ansehen
 (1 Kommentar) (2 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
Domain: Error MessagesHelp WantedPossible Improvement

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

🔎 Search Terms

"statically known members." "could be instantiated with a different subtype of constraint 'object'."

🕗 Version & Regression Information

  • This changed between versions 3.6.2 and 3.7.5

⏯ Playground Link

https://www.typescriptlang.org/play?ts=3.7.5#code/JYOwLgpgTgZghgYwgAgKIA84FsAOAbCAHgBVkJ1IQATAZ2QHsAjAKwgTAD4yKJq7SA3gF8AUEA

💻 Code

interface Example<T extends object> extends T {}

🙁 Actual behavior

I get the following message:

Interface 'Example' incorrectly extends interface 'T'. 'Example' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'object'.

This error message is misleading. The interface does not vary with T and since it declares no new members, is completely compatible with T.

🙂 Expected behavior

I expect a descriptive message. Prior to 3.6.2, the error message was more helpful:

An interface can only extend an object type or intersection of object types with statically known members.

This is still the error you get if the type variable T does not have the extends constraint.

Additional information about the issue

No response

Contributor Guide