microsoft / microsoft/TypeScript
Feature request: typed yield expression
Chưa có ai nhận issue này.
- 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
generator, iterator, yield, type inference, co, redux-saga
Related Issues
#32523
Suggestion
Allow passing of yield operator signature to generator functions
declare function fetchUser(): Promise<User>;
function asyncAction*(yield: <R>(p: Promise<R>) => R) {
const user = yield fetchUser(); // user type inferred to User based on yield param
// impl
}
where yield inside params list is a pseudo-parameter similar to this.
Use Cases
Discussion of use cases is given in #32523.
My intended use is case is emulating do notation for monads with generator functions.
This implies I would normally have some generator runner that does composition defined before writing generator function, so type of expression at which generator resumes is trivially inferrable based on yielded value.
Discussion
The hard thing in typing yield expressions in is that resumed type does not depend only on passed parameters and generator type, but also on context in which the generator is run.
There is a precedent in TypeScript with similar situation -- this parameter in functions.
The situation is analogous -- what this behaves like depends on which object function is member of.
The proposal is to allow declaring contextual info about yield inside generator functions, by introducing appropriate pseudo-param.
I could see this working in two ways:
yieldparam gets erased early, it does not affect generator function signature,
it serves merely as a contract for type-checking / inferrence inside body
function asyncAction*(yield: <R>(p: Promise<R>) => R) {
// impl
}
typeof asyncAction == () => Generator<...>
yieldparam is preserved as type info likethisparam.
then it is always present on types of shape(yield: ...) => Generator<>and defaults to(a: any) => any.
function asyncAction*(yield: <R>(p: Promise<R>) => R) {
// impl
}
typeof asyncAction == (yield: <R>(p: Promise<R>) => R) => Generator<...>
Additionaly
- signature of
yieldmust extend(a: any) => anyand defaults to it
function asyncAction*(yield: number) {
// impl
}
// Error: signature must be of shape (any => any)
- yielded values inside generator function body must be assignable to argument of
yieldsignature
function asyncAction*(yield: (s: string) => void) {
yield 7; // Error: number is not assignable to string
}
- this parameter has no effect on
Generator<>return type, whether declared or
other than obvious coherence guaranteed by (2)
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
thats an opt-in feature - This wouldn't change the runtime behavior of existing JavaScript code
type level feature, doesn't affect runtime behavior - This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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.
Hướng nghiên cứu
Bắt đầu bằng cách đọc issue liên quan #32523 và các ví dụ về generator, iterator và yield trong đề xuất. So sánh hai cách xử lý được đề xuất đối với pseudo-parameter và các quy tắc kiểu của nó; để được xem là hoàn tất, cần có một thiết kế đã được thống nhất, bao quát các giá trị được yield, các kiểu khi tiếp tục, chữ ký hàm và hành vi hiện có của Generator.
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
- 25/100