microsoft / microsoft/TypeScript

Prototype assignment should take the type of the initializer and not require a literal

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

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

Awaiting More Feedback Suggestion
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ả

This is a feature request based on actual VS customer code.

TypeScript Version: 3.9.2

Search Terms: ES5 class javascript prototype assignment constructor function

Expected behavior: In the example below, the prototype for test.class should include properties from testPrototype.

Actual behavior: Per @sandersn this pattern is recognized in the binder and only uses syntactic information, therefore does not use the type information from testPrototype .

Related Issues: #39167 from same user code. Also #33454 maybe?

Code

var test = {};
test.testPrototype = {
    add: function (i) {
    }
}

test.class = function (name) {

    function getName() {
        return name;
    }

    this.name = getName();
}

test.class.prototype = test.testPrototype; 

var t = new test.class("test");
t.name
t.add // EXPECTED: Binds to `add` from the prototype, ACTUAL: doesn't


//
// Same pattern works when a literal is assigned to the prototype:
//

var test2 = {};

test2.class = function (name) {

    function getName() {
        return name;
    }

    this.name = getName();
}

// replaced `test.testPrototype` with a literal 
test2.class.prototype = {
    add: function (i) {
    }
}; 

var t2 = new test2.class("test");
t2.name
t2.add // ACTUAL: `add` bound correctly

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "checkJs": true,
    "allowJs": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

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 với bản tái hiện TypeScript Playground được cung cấp và hành vi của binder được mô tả trong issue, đồng thời so sánh phép gán prototype của object literal với phép gán từ test.testPrototype. Công việc hoàn tất khi kiểu của initializer cung cấp các thuộc tính prototype để t.add được nhận diện trong ví dụ, trong khi trường hợp literal hiện có vẫn tiếp tục hoạt động.

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
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
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.