microsoft / microsoft/TypeScript

[Regression] Circular reference error when passing a class expression with non-primitive static properties to a generic function

Đang mở
#59,271 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.

Bug Domain: check: Type Circularity Help Wanted
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

class expression static type

🕗 Version & Regression Information
  • This changed between versions 4.0.5 and 4.1.5
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=4.1.5#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXzg2RlQElUAHZDAHgBUA+ACi0uoC546BKTugbgBQgsHgDOGeFHgBeAiCIlyVDE0hQxY+AEF4Ab0EBICVGxgkOHLP0BfQTe5A

💻 Code
declare function returnInput<T>(input: T): T;

const a = returnInput(class A {
	static foo = {}
//              ^?
})
🙁 Actual behavior

A.foo: any
Error: 'foo' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)

And also note that this bug only happens when the type of foo is not a primitive type.

🙂 Expected behavior

A.foo: {}, as it works in 4.0.5

Additional information about the issue

To define a React class component, it is a common practice to include its "sub-components" as private static properties. I also use a "observer" function to make the state change auto observed. So the real-world code may look like:

import { observer } from 'mobx-react';
import { styled } from 'styled-components';

@observer
class ListView extends React.Component {
    // render method here...
    private static readonly Item = observer(class Item extends React.Component {
        private static readonly Header = styled.h4` // <- Cannot infer the type of Header
        // CSS style here...
        `;
    })
}

A workaround is to use static block to initialize the static property but it's cumbersome:

const a = returnInput(class A {
	static foo
//              ^? OK
	static { this.foo = {}; }
})

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 TypeScript Playground được liên kết và so sánh hồi quy giữa các phiên bản 4.0.5 và 4.1.5 bằng ví dụ về hàm generic và biểu thức class. Điều tra lý do thuộc tính static không nguyên thủy được suy luận là any, và coi issue là hoàn tất khi ví dụ báo cáo A.foo là {} mà không có lỗi 7022, đồng thời giữ nguyên hành vi workaround đã được ghi lạ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ệ
typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả 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.