microsoft / microsoft/TypeScript

Compilation option: `stricterPropertyInitialization`

Đang mở
#60,906 3 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ả

🔍 Search Terms

Keywords: "stricter property initiazliation"

✅ Viability Checklist
⭐ Suggestion

So, TypeScript offers the strictPropertyInitialization compiler option, which is helpful, but it has this behavior:

class UserAccount {
  name: string; // not an error, initialized in constructor
  accountType = "user"; // initialized
 
  email: string; // this is an error
  address: string | undefined; // NOT an error but ALSO not technically initialized!
 
  constructor(name: string) {
    this.name = name;
  }
}

This is great except for one thing: performance. Uninitialized properties in the class (specifically: address) will, at least in V8, potentially switch the class to dictionary mode vs struct mode when the property eventually gets assigned, leading to slower read/writes. This was confirmed by a V8 developer in this StackOverflow thread.

So the suggestion is that stricterPropertyInitialization sets strictPropertyInitialization to true AND throws an error if the author has added | undefined to the type to implicity define the type. In other words, "strict property initialization" would mean what it says: strict property initialization. If it is not initialized, it is an error. In the above example, from the tsconfig docs, the address is not initialized. It's too late to change that as default behavior, hence the additional flag.

Note: maybe stricterPropertyInitialization is too awkward? Something like disallowImplicitUndefinedClassFields? 🤷‍♂

📃 Motivating Example

Most devs probably won't need to worry about the performance implications of internal dictionary vs. struct. In my case, in the library I'm maintaining / working on, every millisecond / fraction of a millisecond counts, so I'm trying to determine the fastest path in every scenario. I expected strictPropertyInitialization to mean what it says to prevent properties from not being initialized and avoid any accidental performance pitfalls from non-initialization, but it doesn't. 🤷‍♂

Meaning, in the above example:

class UserAccount {
  name: string; // not an error, initialized in constructor
  accountType = "user"; // initialized
 
  email: string; // this is an error
  address: string | undefined; // NOT an error but ALSO not technically initialized!
 
  constructor(name: string) {
    this.name = name;
  }

  someMethod() {
    // The class will now possible de-optimize its performance to Dictionary mode, since V8
    // thinks that the object needs to have arbitrary properties added. It doesn't know about `address`
    this.address = '123 fake street' 
  }
}

So, most simply, stricterPropertyInitialization would throw a compilation error if | undefined is added to a property and the property is not explicitly initialized with a field initializer or in the constructor.

If the code author wishes to actually initialize the field, to undefined, it must be something like:

class UserAccount {
  // ...
  address: string | undefined = undefined;
 
  constructor(name: string) {
    this.name = name;
  }
}
💻 Use Cases
  1. What do you want to use this for?
    A high-performance TS/JS library

  2. What shortcomings exist with current approaches?
    There are no workarounds as far as I know. You simply have to let all devs on your team know to not write this and hope people catch it.

  3. What workarounds are you using in the meantime?
    Vigilance

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

Issue không nêu tên tệp, test hoặc entry point nào. Hãy bắt đầu bằng việc xác định phần triển khai và các test cho strictPropertyInitialization, sau đó xác định một tùy chọn riêng nên xử lý các field chưa được khởi tạo có kiểu | undefined như thế nào so với các field được khởi tạo rõ ràng. Công việc được xem là hoàn tất khi hành vi và tên của tùy chọn mới đã được thống nhất, đồng thời các ví dụ UserAccount được bao phủ bởi các compiler test.

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

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ 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.