microsoft / microsoft/TypeScript

JSDoc tag for getting around "Object literals are open-ended"

Đang mở
#63,691 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.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

🔍 Search Terms

label:"Domain: JSDoc" open-ended
label:"Domain: JSDoc" literal
label:checkJs open-ended
label:checkJs literal

✅ Viability Checklist
⭐ Suggestion

According to the documentation, TypeScript treats inferred object literals in checked .js files as open-ended by default. This is good and makes sense, but is annoying for the times when it isn't the desired functionality.

I'd like for a new tag to be introduced. I'll be using @closed as an example, but I'm fine with whatever wording, really. The new tag would be used on an object definition in order to opt out of this open-ended behavior for that specific object literal declaration.

Ideally, this would be done recursively through the object's definition (i.e. I shouldn't have to define @closed for each sub-object), but I understand if that's too broad-reaching. I'd be fine with having that limitation.

This is obviously very similar to Exact Types (#12936), but it concerns general exact object types and structural compatibility, whereas this request is limited to a JSDoc opt-in for inferred object literals in checked JavaScript. I therefore believe this warrants its own feature request.

📃 Motivating Example

I use JSDoc quite extensively in my hobby work. I often run into issues like this:

class CustomFooBarElement extends HTMLElement {
  _elements = {
    /** @type {HTMLSpanElement|null} */
    foo: null,
  };

  constructor() {
    super();
    this._elements.bar = document.createElement("span");
    //             ^ currently doesn't report an error
  }
}

Now, you can easily work around this by adding a full object typing to the _elements object:

class CustomFooBarElement extends HTMLElement {
  /** @type {{foo: HTMLSpanElement|null}} */
  _elements = {
    foo: null,
  };

which would cause the .bar access to fail. However, maintaining such a declaration can quickly grow cumbersome when the _elements object grows in size and complexity:

class CustomFooBarElement extends HTMLElement {
  /**
    * @type {{
    *   foo: HTMLSpanElement | null,
    *   buttons: {
    *     primary: {
    *       a: HTMLButtonElement | null,
    *       b: HTMLButtonElement | null,
    *     },
    *   },
    *   images: {
    *     a: HTMLImageElement | null,
    *     b: HTMLImageElement | null,
    *   },
    * }}
    */
  _elements = {
    foo: null,
    buttons: {
        primary: {
            a: null,
            b: null,
        },
    },
    images: {
        a: null,
        b: null,
    },
  };

A silly example, but I think you get the point. Not to mention the fact that you've completely decoupled the type definition from the definition of the variable itself.

Now, if I instead could do something like this:

class CustomFooBarElement extends HTMLElement {
  /** @closed */
  _elements = {
    /** @type {HTMLSpanElement|null} */
    foo: null,
    buttons: {
        primary: {
            /** @type {HTMLButtonElement|null} */
            a: null,
            /** @type {HTMLButtonElement|null} */
            b: null,
        },
    },
    images: {
        /** @type {HTMLImageElement|null} */
        a: null,
        /** @type {HTMLImageElement|null} */
        b: null,
    },
  };

It's much easier to handle, and would lead to the desired result:

this._elements.bar = document.createElement("span");
//             ^^^ error: Property 'bar' does not exist

this._elements.buttons.primary.c = document.createElement("button");
//                                     ^ error: Property 'c' does not exist
💻 Use Cases
  1. What do you want to use this for?

Typing fixed-shape objects. See the examples above.

  1. What shortcomings exist with current approaches?

The object-declaration-level @type requires essentially repeating the entire structure of the object. It also decouples the type of the actual leaf property from its definition.

  1. What workarounds are you using in the meantime?

I started with the object-declaration-level @type, but at this point I just use the leaf-level @type and hope I don't run into any spelling mistakes or such.

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 phần handbook “Object literals are open-ended” và Exact Types issue được liên kết (#12936), sau đó điều tra hành vi JSDoc của checked-JavaScript được mô tả trong các ví dụ. Công việc được xem là hoàn tất khi cung cấp một opt-in JSDoc được ghi chép cho các object literal được suy luận có hình dạng cố định, bao gồm phạm vi đã thống nhất cho các object lồng nhau, và các phép gán thuộc tính bổ sung được minh họa bị từ chối.

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ó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/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.