microsoft / microsoft/TypeScript

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

オープン
#19,397 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Domain: This-Typing
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供されたsequelize宣言を使い、名前付きインポート構文でModelをインポートして問題を再現します。polymorphic thisをチェックする際に、コンパイラーがエクスポートされた変数と同名のinterfaceをどのように解決するかを追跡します。完了条件は、例でModelが推論され、namespace-import workaroundを必要とせずにチェーン呼び出しが許可されることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。