microsoft / microsoft/TypeScript

Polymorphic `this` breaks when a module exports a variable and interface with the same name

Open
#19,397 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Importing a variable that has a corresponding interface of the same name from a module causes unexpected behavior.

In the screenshot below, the function, doSomething, should return the type Model<number> due to polymorphic this; however, it returns Model<T>, which is unusable.

screen shot 2017-10-20 at 11 02 03 pm

Here is an easy way to reproduce this behavior:

  1. npm install -g @types/sequelize sequelize
  2. Replace node_modules/@types/sequelize/index.d.ts with the following
declare namespace sequelize {
    interface Model<T>  {
        doSomething(arg: T): this;
    }
}

declare var sequelize: { Model: sequelize.Model<any> };

export = sequelize;
  1. create a file with the following contents:
import { Model } from 'sequelize';

let a: Model<number> = null as any;


a.doSomething(5).doSomething();

Workaround:
Use import * as ... syntax and the names will resolve correctly.

screen shot 2017-10-20 at 11 22 28 pm

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 by reproducing the issue with the provided sequelize declaration and importing Model with named-import syntax. Trace how the compiler resolves the exported variable and same-named interface when checking polymorphic this. Done means the example infers Model and permits the chained call without requiring the namespace-import workaround.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.