microsoft / microsoft/TypeScript
Generators with extended unions, null and undefined produces compiler errors
@rbuckton đang làm issue này rồi.
Từ ngày 3/9/2020.
- 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ả
I've run into a weird problem in a project that uses async generators. I've reduced the problematic code to the example
below, which also fails in a similar way that my actual code does.
There seem to be several conditions that must be fulfilled:
- Type
Tis a union. - The argument generator extends
T(withWithIDin the example). - The argument generator also accepts both
nullandundefiened. - The provided generator has a return signature.
- The provided generator's return signature yields
Tand eithernullorundefined(but not both).
Change just any of these conditions in the playground, and the error goes away ...
TypeScript Version: 4.0.0-beta (and below)
Search Terms: generator null undefined
Expected behavior: No errors.
Actual behavior: A strange error message when doSomething() is called.
Related Issues: No
Code
interface WithID {
id?: number;
}
type MyEvent = { type: "number", value: number } | { type: "string", value: string };
//interface MyEvent { type: "number" | "string", value: number | string };
function doSomething<T extends object>(gen: Generator<T & WithID | undefined | null>) {
// ...
}
function* readEvents(): Generator<MyEvent | undefined> {
yield undefined;
}
doSomething(readEvents());
Output
"use strict";
//interface MyEvent { type: "number" | "string", value: number | string };
function doSomething(gen) {
// ...
}
function* readEvents() {
yield undefined;
}
doSomething(readEvents());
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": 2,
"target": "ES2019",
"jsx": "React",
"module": "ESNext"
}
}
Playground Link: Provided
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.
Đánh giá
Issue này chưa được đánh giá.