Skip to content

Commit e48bcd6

Browse files
author
Andy
authored
Treat 'yield;' as 'yield undefined;' (#22297)
* Treat 'yield;' as 'yield undefined;' * Use undefinedWideningType
1 parent 28e8c4f commit e48bcd6

11 files changed

+121
-25
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18525,9 +18525,7 @@ namespace ts {
1852518525
const aggregatedTypes: Type[] = [];
1852618526
const isAsync = (getFunctionFlags(func) & FunctionFlags.Async) !== 0;
1852718527
forEachYieldExpression(<Block>func.body, yieldExpression => {
18528-
if (yieldExpression.expression) { // TODO: GH#22297
18529-
pushIfUnique(aggregatedTypes, getYieldedTypeOfYieldExpression(yieldExpression, isAsync, checkMode));
18530-
}
18528+
pushIfUnique(aggregatedTypes, getYieldedTypeOfYieldExpression(yieldExpression, isAsync, checkMode));
1853118529
});
1853218530
return aggregatedTypes;
1853318531
}
@@ -19520,8 +19518,6 @@ namespace ts {
1952019518
}
1952119519
}
1952219520

19523-
if (!node.expression) return anyType; // TODO: GH#22297
19524-
1952519521
const isAsync = (functionFlags & FunctionFlags.Async) !== 0;
1952619522
const yieldedType = getYieldedTypeOfYieldExpression(node, isAsync);
1952719523
// There is no point in doing an assignability check if the function
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck48.ts(1,9): error TS7025: Generator implicitly has type 'IterableIterator<any>' because it does not yield any values. Consider supplying a return type.
1+
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck48.ts(1,11): error TS7010: 'g', which lacks return-type annotation, implicitly has an 'any' return type.
2+
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck48.ts(5,11): error TS7010: 'h', which lacks return-type annotation, implicitly has an 'any' return type.
23

34

4-
==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck48.ts (1 errors) ====
5+
==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck48.ts (2 errors) ====
56
function* g() {
6-
~
7-
!!! error TS7025: Generator implicitly has type 'IterableIterator<any>' because it does not yield any values. Consider supplying a return type.
7+
~
8+
!!! error TS7010: 'g', which lacks return-type annotation, implicitly has an 'any' return type.
89
yield;
9-
}
10+
}
11+
12+
function* h() {
13+
~
14+
!!! error TS7010: 'h', which lacks return-type annotation, implicitly has an 'any' return type.
15+
yield undefined;
16+
}
17+
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
//// [generatorTypeCheck48.ts]
22
function* g() {
33
yield;
4-
}
4+
}
5+
6+
function* h() {
7+
yield undefined;
8+
}
9+
510

611
//// [generatorTypeCheck48.js]
712
function* g() {
813
yield;
914
}
15+
function* h() {
16+
yield undefined;
17+
}

tests/baselines/reference/generatorTypeCheck48.symbols

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ function* g() {
44

55
yield;
66
}
7+
8+
function* h() {
9+
>h : Symbol(h, Decl(generatorTypeCheck48.ts, 2, 1))
10+
11+
yield undefined;
12+
>undefined : Symbol(undefined)
13+
}
14+

tests/baselines/reference/generatorTypeCheck48.types

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@ function* g() {
55
yield;
66
>yield : any
77
}
8+
9+
function* h() {
10+
>h : () => IterableIterator<any>
11+
12+
yield undefined;
13+
>yield undefined : any
14+
>undefined : undefined
15+
}
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/compiler/yieldExpression1.ts(7,5): error TS2322: Type 'undefined' is not assignable to type 'number'.
2+
3+
4+
==== tests/cases/compiler/yieldExpression1.ts (1 errors) ====
5+
function* a() {
6+
yield;
7+
yield 0;
8+
}
9+
10+
function* b(): IterableIterator<number> {
11+
yield;
12+
~~~~~
13+
!!! error TS2322: Type 'undefined' is not assignable to type 'number'.
14+
yield 0;
15+
}
16+
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
//// [yieldExpression1.ts]
2-
function* foo() {
3-
yield
4-
}
2+
function* a() {
3+
yield;
4+
yield 0;
5+
}
6+
7+
function* b(): IterableIterator<number> {
8+
yield;
9+
yield 0;
10+
}
11+
512

613
//// [yieldExpression1.js]
7-
function* foo() {
14+
function* a() {
15+
yield;
16+
yield 0;
17+
}
18+
function* b() {
819
yield;
20+
yield 0;
921
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
=== tests/cases/compiler/yieldExpression1.ts ===
2-
function* foo() {
3-
>foo : Symbol(foo, Decl(yieldExpression1.ts, 0, 0))
2+
function* a() {
3+
>a : Symbol(a, Decl(yieldExpression1.ts, 0, 0))
44

5-
yield
5+
yield;
6+
yield 0;
67
}
8+
9+
function* b(): IterableIterator<number> {
10+
>b : Symbol(b, Decl(yieldExpression1.ts, 3, 1))
11+
>IterableIterator : Symbol(IterableIterator, Decl(lib.es2015.iterable.d.ts, --, --))
12+
13+
yield;
14+
yield 0;
15+
}
16+
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
=== tests/cases/compiler/yieldExpression1.ts ===
2-
function* foo() {
3-
>foo : () => IterableIterator<any>
2+
function* a() {
3+
>a : () => IterableIterator<0 | undefined>
44

5-
yield
5+
yield;
66
>yield : any
7+
8+
yield 0;
9+
>yield 0 : any
10+
>0 : 0
711
}
12+
13+
function* b(): IterableIterator<number> {
14+
>b : () => IterableIterator<number>
15+
>IterableIterator : IterableIterator<T>
16+
17+
yield;
18+
>yield : any
19+
20+
yield 0;
21+
>yield 0 : any
22+
>0 : 0
23+
}
24+
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
// @target: es6
2-
function* foo() {
3-
yield
4-
}
2+
// @strictNullChecks: true
3+
4+
function* a() {
5+
yield;
6+
yield 0;
7+
}
8+
9+
function* b(): IterableIterator<number> {
10+
yield;
11+
yield 0;
12+
}

tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck48.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33

44
function* g() {
55
yield;
6-
}
6+
}
7+
8+
function* h() {
9+
yield undefined;
10+
}

0 commit comments

Comments
 (0)