Skip to content

Commit d6bdb1a

Browse files
committed
Additional contextual type tests
1 parent fdf0095 commit d6bdb1a

File tree

6 files changed

+114
-3
lines changed

6 files changed

+114
-3
lines changed

tests/baselines/reference/asyncYieldStarContextualType.symbols

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,25 @@ declare const mapper: <T>(result: Result<T, "NOT_FOUND_AUTHOR">) => Result<T, "N
4949
>Result : Symbol(Result, Decl(asyncYieldStarContextualType.ts, 0, 0))
5050
>T : Symbol(T, Decl(asyncYieldStarContextualType.ts, 10, 23))
5151

52+
declare const g: <T, U, V>() => AsyncGenerator<T, U, V>;
53+
>g : Symbol(g, Decl(asyncYieldStarContextualType.ts, 11, 13))
54+
>T : Symbol(T, Decl(asyncYieldStarContextualType.ts, 11, 18))
55+
>U : Symbol(U, Decl(asyncYieldStarContextualType.ts, 11, 20))
56+
>V : Symbol(V, Decl(asyncYieldStarContextualType.ts, 11, 23))
57+
>AsyncGenerator : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --))
58+
>T : Symbol(T, Decl(asyncYieldStarContextualType.ts, 11, 18))
59+
>U : Symbol(U, Decl(asyncYieldStarContextualType.ts, 11, 20))
60+
>V : Symbol(V, Decl(asyncYieldStarContextualType.ts, 11, 23))
61+
5262
async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookWithAuthor, unknown> {
53-
>f : Symbol(f, Decl(asyncYieldStarContextualType.ts, 10, 98))
63+
>f : Symbol(f, Decl(asyncYieldStarContextualType.ts, 11, 56))
5464
>AsyncGenerator : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --))
5565
>BookWithAuthor : Symbol(BookWithAuthor, Decl(asyncYieldStarContextualType.ts, 6, 43))
5666

5767
// Without yield*, the type of test1 is
5868
// Result<Author, "NOT_FOUND_AUTHOR>
5969
const test1 = await authorPromise.then(mapper)
60-
>test1 : Symbol(test1, Decl(asyncYieldStarContextualType.ts, 15, 9))
70+
>test1 : Symbol(test1, Decl(asyncYieldStarContextualType.ts, 16, 9))
6171
>authorPromise.then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
6272
>authorPromise : Symbol(authorPromise, Decl(asyncYieldStarContextualType.ts, 9, 13))
6373
>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
@@ -67,12 +77,20 @@ async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookW
6777
// Author | BookWithAuthor
6878
// But this codepath has no way to produce BookWithAuthor
6979
const test2 = yield* await authorPromise.then(mapper)
70-
>test2 : Symbol(test2, Decl(asyncYieldStarContextualType.ts, 20, 9))
80+
>test2 : Symbol(test2, Decl(asyncYieldStarContextualType.ts, 21, 9))
7181
>authorPromise.then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
7282
>authorPromise : Symbol(authorPromise, Decl(asyncYieldStarContextualType.ts, 9, 13))
7383
>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
7484
>mapper : Symbol(mapper, Decl(asyncYieldStarContextualType.ts, 10, 13))
7585

86+
const x1 = yield* g();
87+
>x1 : Symbol(x1, Decl(asyncYieldStarContextualType.ts, 23, 9))
88+
>g : Symbol(g, Decl(asyncYieldStarContextualType.ts, 11, 13))
89+
90+
const x2: number = yield* g();
91+
>x2 : Symbol(x2, Decl(asyncYieldStarContextualType.ts, 24, 9))
92+
>g : Symbol(g, Decl(asyncYieldStarContextualType.ts, 11, 13))
93+
7694
return null! as BookWithAuthor;
7795
>BookWithAuthor : Symbol(BookWithAuthor, Decl(asyncYieldStarContextualType.ts, 6, 43))
7896
}

tests/baselines/reference/asyncYieldStarContextualType.types

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ declare const mapper: <T>(result: Result<T, "NOT_FOUND_AUTHOR">) => Result<T, "N
4848
>result : Result<T, "NOT_FOUND_AUTHOR">
4949
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

51+
declare const g: <T, U, V>() => AsyncGenerator<T, U, V>;
52+
>g : <T, U, V>() => AsyncGenerator<T, U, V>
53+
> : ^ ^^ ^^ ^^^^^^^
54+
5155
async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookWithAuthor, unknown> {
5256
>f : () => AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookWithAuthor, unknown>
5357
> : ^^^^^^
@@ -91,6 +95,26 @@ async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookW
9195
>mapper : <T>(result: Result<T, "NOT_FOUND_AUTHOR">) => Result<T, "NOT_FOUND_AUTHOR">
9296
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9397

98+
const x1 = yield* g();
99+
>x1 : unknown
100+
> : ^^^^^^^
101+
>yield* g() : unknown
102+
> : ^^^^^^^
103+
>g() : AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", unknown, unknown>
104+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
105+
>g : <T, U, V>() => AsyncGenerator<T, U, V>
106+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107+
108+
const x2: number = yield* g();
109+
>x2 : number
110+
> : ^^^^^^
111+
>yield* g() : number
112+
> : ^^^^^^
113+
>g() : AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", number, unknown>
114+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115+
>g : <T, U, V>() => AsyncGenerator<T, U, V>
116+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117+
94118
return null! as BookWithAuthor;
95119
>null! as BookWithAuthor : BookWithAuthor
96120
> : ^^^^^^^^^^^^^^
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [tests/cases/compiler/yieldStarContextualType.ts] ////
2+
3+
=== yieldStarContextualType.ts ===
4+
declare const g: <T, U, V>() => Generator<T, U, V>;
5+
>g : Symbol(g, Decl(yieldStarContextualType.ts, 0, 13))
6+
>T : Symbol(T, Decl(yieldStarContextualType.ts, 0, 18))
7+
>U : Symbol(U, Decl(yieldStarContextualType.ts, 0, 20))
8+
>V : Symbol(V, Decl(yieldStarContextualType.ts, 0, 23))
9+
>Generator : Symbol(Generator, Decl(lib.es2015.generator.d.ts, --, --))
10+
>T : Symbol(T, Decl(yieldStarContextualType.ts, 0, 18))
11+
>U : Symbol(U, Decl(yieldStarContextualType.ts, 0, 20))
12+
>V : Symbol(V, Decl(yieldStarContextualType.ts, 0, 23))
13+
14+
function* f(): Generator<string, void, unknown> {
15+
>f : Symbol(f, Decl(yieldStarContextualType.ts, 0, 51))
16+
>Generator : Symbol(Generator, Decl(lib.es2015.generator.d.ts, --, --))
17+
18+
const x1 = yield* g();
19+
>x1 : Symbol(x1, Decl(yieldStarContextualType.ts, 3, 9))
20+
>g : Symbol(g, Decl(yieldStarContextualType.ts, 0, 13))
21+
22+
const x2: number = yield* g();
23+
>x2 : Symbol(x2, Decl(yieldStarContextualType.ts, 4, 9))
24+
>g : Symbol(g, Decl(yieldStarContextualType.ts, 0, 13))
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/yieldStarContextualType.ts] ////
2+
3+
=== yieldStarContextualType.ts ===
4+
declare const g: <T, U, V>() => Generator<T, U, V>;
5+
>g : <T, U, V>() => Generator<T, U, V>
6+
> : ^ ^^ ^^ ^^^^^^^
7+
8+
function* f(): Generator<string, void, unknown> {
9+
>f : () => Generator<string, void, unknown>
10+
> : ^^^^^^
11+
12+
const x1 = yield* g();
13+
>x1 : unknown
14+
> : ^^^^^^^
15+
>yield* g() : unknown
16+
> : ^^^^^^^
17+
>g() : Generator<string, unknown, unknown>
18+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
>g : <T, U, V>() => Generator<T, U, V>
20+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
22+
const x2: number = yield* g();
23+
>x2 : number
24+
> : ^^^^^^
25+
>yield* g() : number
26+
> : ^^^^^^
27+
>g() : Generator<string, number, unknown>
28+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
>g : <T, U, V>() => Generator<T, U, V>
30+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
}

tests/cases/compiler/asyncYieldStarContextualType.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type BookWithAuthor = Book & { author: Author };
1212

1313
declare const authorPromise: Promise<Result<Author, "NOT_FOUND_AUTHOR">>;
1414
declare const mapper: <T>(result: Result<T, "NOT_FOUND_AUTHOR">) => Result<T, "NOT_FOUND_AUTHOR">;
15+
declare const g: <T, U, V>() => AsyncGenerator<T, U, V>;
1516

1617
async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookWithAuthor, unknown> {
1718
// Without yield*, the type of test1 is
@@ -23,5 +24,8 @@ async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookW
2324
// But this codepath has no way to produce BookWithAuthor
2425
const test2 = yield* await authorPromise.then(mapper)
2526

27+
const x1 = yield* g();
28+
const x2: number = yield* g();
29+
2630
return null! as BookWithAuthor;
2731
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @target: esnext
2+
// @noEmit: true
3+
4+
declare const g: <T, U, V>() => Generator<T, U, V>;
5+
6+
function* f(): Generator<string, void, unknown> {
7+
const x1 = yield* g();
8+
const x2: number = yield* g();
9+
}

0 commit comments

Comments
 (0)