microsoft / microsoft/TypeScript
Generators with extended unions, null and undefined produces compiler errors
オープン
@rbuckton がすでに取り組んでいます。
2020年9月3日 から。
Needs Investigation
Rescheduled
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
評価
この issue はまだ評価されていません。