Object with all context-sensitive properties requires at least one non-context-sensitive property for inference to work
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript
- Lĩnh vực
- compilers
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện sự khác biệt về suy luận trong TypeScript Playground được liên kết, sử dụng hai ví dụ createMachine. Đọc khai báo generic và kiểu IdentityObject trong issue, sau đó theo dõi quá trình trình biên dịch suy luận tham số kiểu cho thuộc tính entry phụ thuộc vào ngữ cảnh. Được xem là hoàn tất khi phiên bản không có thuộc tính _ bổ sung suy luận context là { foo: number } và state là "a".
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
🔎 Search Terms
object with all context-sensitive properties, self referencial types, xstate
🕗 Version & Regression Information
Tested with TypeScript 7.0.2, the issue exists even in versions as old as 4.4.4
⏯ Playground Link
💻 Code
// problem: doesn't infer D correctly, context and state are any
createMachine({
initial: "a",
context: { foo: 1 },
states: {
a: {
entry: (context, state) => {
context
// ^? any
state
// ^? any
}
}
}
})
// solution: add a useless non-context-sensitive property `_: null`
createMachine({
initial: "a",
context: { foo: 1 },
states: {
a: {
entry: (context, state) => {
context
// ^? { foo: number }
state
// ^? "a"
},
_: null
}
}
})
declare const createMachine:
<D extends {
initial: keyof D["states"],
context: object,
states: {
[S in keyof D["states"]]: {
entry?: (context: D["context"], state: S) => void,
_?: null
}
}
}>
(definition: IdentityObject<D>) =>
D
type Identity<T> =
T extends any
? ( T extends (...a: never) => unknown ? T :
T extends object ? IdentityObject<T> :
T
)
: never
type IdentityObject<T> =
{ [K in keyof T]: Identity<T[K]> }
🙁 Actual behavior
The first createMachine invocation doesn't infer the type parameter correctly (it requires at least one non-context-sensitive property) and hence context and state parameters of entry are not inferred.
🙂 Expected behavior
The first createMachine invocation should infer the type parameter correctly without having to pass in a non-context-sensitive property leading to inference of context and state parameters of entry
Additional information about the issue
The premise is adding (or rather not adding) an extra useless property shouldn't make a difference to the inference
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 15 giờ
- Pull request đã merge (30 ngày)
- 106
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/TypeScript
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64322 · 2 bình luận · 1 reaction · 2 người được giao ·
-
Possible Improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
microsoft/TypeScript#64278 · 1 bình luận · 1 reaction ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
microsoft/TypeScript#64118 · 1 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64094 ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
microsoft/TypeScript#63959 · 5 bình luận ·
Tất cả issue của microsoft/TypeScript
Issue tương tự
-
Type/Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
OpenNSW/nsw-srilanka#497 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
kubernetes-sigs/prow#953 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
caddyserver/caddy#8046 ·