microsoft / microsoft/TypeScript

Misleading error message when interface tries to extend constrained type parameter

Open
#57,037 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Error Messages Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript 3.7.5 Playground example: interface Example extends T {}. Compare its diagnostic with the unconstrained type-parameter case and the pre-3.6.2 wording. Done means the constrained case reports a descriptive diagnostic without regressing the existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.