microsoft / microsoft/TypeScript

Improve Javascript intellisense type inference for cases where Object.assign(this, ...) is used with an object with known type information

オープン
#16,163 コメント 7 件 リアクション 23 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Domain: JavaScript Suggestion VS Code Tracked
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

From @jj101k on May 28, 2017 10:16

  • VSCode Version: 1.12.2 (19222cdc84ce72202478ba1cec5cb557b71163de)
  • OS Version: macOS Sierra 10.12.5 (16F73)

Given the Javascript code:

class Foo {
    constructor() {
        Object.assign(
            this,
            {
                bar: "abc",
            }
        );
        this.foo = "def";
    }
}

var f = new Foo();
console.log(f.foo);
console.log(f.bar);

If you hover over f.foo, it will tell you that it's a string. If you hover over f.bar, it will say it's "any". They should both say "string".

Object.assign is a fairly common way of setting several properties with a bit less copy/paste. In cases where an object without known type information is used as the last argument it isn't possible to statically infer appropriate types for the modified object ("this", here) and equivalently the object that Object.assign returns. Where type information of the last argument is known, it is safe and appropriate to import all of that into the type information for the object, as if a series of direct assignments had been done.

Caveats: In Javascript (perhaps not Typescript) it's possible that further unknown properties are present on any given object, so it would be appropriate to void all inferred type information which is not between Object.assign and the end of the constructor. If Object.assign is used outside a constructor (on a named object) it should void all inferred types entirely, because the type information could be entirely different before and after. The same should be true if Object.assign is conditionally called in the constructor. The only exception should be when all arguments after the first have known final type information, eg. an immediate object.

For practical purposes, Object.assign(foo, bar, {baz: 1}) should be considered equivalent to for(name in bar) if(bar.hasOwnProperty(name)) foo[name] = bar[name]; foo.baz=1;.

Copied from original issue: Microsoft/vscode#27397

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

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

はじめの一歩

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

調査の方向性

まず、提供された JavaScript クラスと Object.assign の例を TypeScript checker またはエディターツールで再現し、次に最後の引数の既知の型がどのように処理されるかを追跡します。f.foo と f.bar の両方にカーソルを合わせたときに string と表示され、unknown、条件付き、または後続の代入について文書化された注意点が維持されれば完了です。

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

評価

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

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

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