microsoft / microsoft/TypeScript

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

Đang mở
#16,163 7 bình luận 23 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Domain: JavaScript Suggestion VS Code Tracked
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện class JavaScript và ví dụ Object.assign được cung cấp trong trình kiểm tra TypeScript hoặc các công cụ của trình soạn thảo, sau đó theo dõi cách kiểu đã biết của đối số cuối cùng được xử lý. Được xem là hoàn tất khi di con trỏ lên cả f.foo và f.bar đều hiển thị string, đồng thời vẫn giữ các lưu ý đã được ghi tài liệu cho những phép gán không xác định, có điều kiện hoặc xảy ra sau đó.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.