microsoft / microsoft/TypeScript

Interface definition utilizing `this` type errors out only in specific cases and only when a member is accessed, not before

Open
#43,613 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: This-Typing Needs Human Review
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Bug Report

🔎 Search Terms

"interface this"
"interface this" intersection
"is referenced directly or indirectly"
"is referenced directly or indirectly in its own type annotation."
ts(2502)
HKT circular
intersection circular

🕗 Version & Regression Information
  • This changed between versions 3.8.3 and 3.9.7
⏯ Playground Link

Playground link with relevant minimal code and commented version

💻 Code

With version 4.2.3

interface foo<T> { a: T; b: this["a"] };
//                       ^- 'b' is referenced directly or indirectly in its own type annotation.ts(2502)
type intersection = foo<string> & foo<number>; // Works in some cases, shown in linked playground
type induces_error = intersection["a"]; // Comment this out and the error in the interface goes away

Found when messing with ways to simulate HKTs with this type, which itself seems to be considered a valid use case, per:
https://github.com/microsoft/TypeScript/issues/40928 https://github.com/microsoft/TypeScript/pull/40967

🙁 Actual behavior
  • Interface definition erroring out for an unclear reason when using said interface later down the line, but not before. It doesn't seem right that the definition shows an error that wasn't there before by just accessing a property.
  • Error itself is questionable, worked in 3.8.3, and a similar use case seems to be covered by the test (#40967) linked above?
🙂 Expected behavior
  • Interface definition either always errors, or an error doesn't pop up only when using the interface definition only in specific ways.
  • Additionally, reason for circularity error is not immediately obvious to me, and it seems it might a regression since 3.8.3?

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 linked TypeScript Playground and the minimal foo<T>/intersection example, reproducing it across the mentioned TypeScript versions 3.8.3, 3.9.7, and 4.2.3. Read the related context in issues #40928 and pull request #40967; done means the behavior is consistently diagnosed and the reported regression or circularity is resolved or clearly explained.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.