microsoft / microsoft/TypeScript
Polymorphic `this` breaks when a module exports a variable and interface with the same name
Nobody has claimed this yet.
- 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.
Here is an easy way to reproduce this behavior:
npm install -g @types/sequelize sequelize- Replace
node_modules/@types/sequelize/index.d.tswith the following
declare namespace sequelize {
interface Model<T> {
doSomething(arg: T): this;
}
}
declare var sequelize: { Model: sequelize.Model<any> };
export = sequelize;
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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