microsoft / microsoft/TypeScript

--esModuleInterop: Strange incompatibility between correct and incorrect import of same type

Open
#21,653 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Error Messages Experience Enhancement In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: master

Code

b.d.ts

declare class C {
    f: this;
}
declare namespace C {}
export = C;

a.ts

import * as C1 from "./b";
import C2 = require("./b");

function f(t: (c: C1) => void): void {
    t(new C2());
}

Expected behavior:

Error at import * as C1.

Actual behavior:

No error at the import declaration. Error at new C2():

src/a.ts(5,7): error TS2345: Argument of type 'C' is not assignable to parameter of type 'C'.
  Types of property 'f' are incompatible.
    Type 'C' is not assignable to type 'this'.

(possibly related: #20704)

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

Reproduce the report using b.d.ts and a.ts exactly as shown, then run the TypeScript checker to observe the diagnostic at new C2(). Start by tracing how import * as C1, import-equals require, export =, and the this-typed property are compared. Done means the incompatibility is handled consistently, with the expected diagnostic location or behavior covered by a regression test.

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
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.