Skip to content

Commit

Permalink
Additional contextual type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed May 22, 2024
1 parent fdf0095 commit d6bdb1a
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
24 changes: 21 additions & 3 deletions tests/baselines/reference/asyncYieldStarContextualType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,25 @@ declare const mapper: <T>(result: Result<T, "NOT_FOUND_AUTHOR">) => Result<T, "N
>Result : Symbol(Result, Decl(asyncYieldStarContextualType.ts, 0, 0))
>T : Symbol(T, Decl(asyncYieldStarContextualType.ts, 10, 23))

declare const g: <T, U, V>() => AsyncGenerator<T, U, V>;
>g : Symbol(g, Decl(asyncYieldStarContextualType.ts, 11, 13))
>T : Symbol(T, Decl(asyncYieldStarContextualType.ts, 11, 18))
>U : Symbol(U, Decl(asyncYieldStarContextualType.ts, 11, 20))
>V : Symbol(V, Decl(asyncYieldStarContextualType.ts, 11, 23))
>AsyncGenerator : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --))
>T : Symbol(T, Decl(asyncYieldStarContextualType.ts, 11, 18))
>U : Symbol(U, Decl(asyncYieldStarContextualType.ts, 11, 20))
>V : Symbol(V, Decl(asyncYieldStarContextualType.ts, 11, 23))

async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookWithAuthor, unknown> {
>f : Symbol(f, Decl(asyncYieldStarContextualType.ts, 10, 98))
>f : Symbol(f, Decl(asyncYieldStarContextualType.ts, 11, 56))
>AsyncGenerator : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --))
>BookWithAuthor : Symbol(BookWithAuthor, Decl(asyncYieldStarContextualType.ts, 6, 43))

// Without yield*, the type of test1 is
// Result<Author, "NOT_FOUND_AUTHOR>
const test1 = await authorPromise.then(mapper)
>test1 : Symbol(test1, Decl(asyncYieldStarContextualType.ts, 15, 9))
>test1 : Symbol(test1, Decl(asyncYieldStarContextualType.ts, 16, 9))
>authorPromise.then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
>authorPromise : Symbol(authorPromise, Decl(asyncYieldStarContextualType.ts, 9, 13))
>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
Expand All @@ -67,12 +77,20 @@ async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookW
// Author | BookWithAuthor
// But this codepath has no way to produce BookWithAuthor
const test2 = yield* await authorPromise.then(mapper)
>test2 : Symbol(test2, Decl(asyncYieldStarContextualType.ts, 20, 9))
>test2 : Symbol(test2, Decl(asyncYieldStarContextualType.ts, 21, 9))
>authorPromise.then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
>authorPromise : Symbol(authorPromise, Decl(asyncYieldStarContextualType.ts, 9, 13))
>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
>mapper : Symbol(mapper, Decl(asyncYieldStarContextualType.ts, 10, 13))

const x1 = yield* g();
>x1 : Symbol(x1, Decl(asyncYieldStarContextualType.ts, 23, 9))
>g : Symbol(g, Decl(asyncYieldStarContextualType.ts, 11, 13))

const x2: number = yield* g();
>x2 : Symbol(x2, Decl(asyncYieldStarContextualType.ts, 24, 9))
>g : Symbol(g, Decl(asyncYieldStarContextualType.ts, 11, 13))

return null! as BookWithAuthor;
>BookWithAuthor : Symbol(BookWithAuthor, Decl(asyncYieldStarContextualType.ts, 6, 43))
}
24 changes: 24 additions & 0 deletions tests/baselines/reference/asyncYieldStarContextualType.types
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ declare const mapper: <T>(result: Result<T, "NOT_FOUND_AUTHOR">) => Result<T, "N
>result : Result<T, "NOT_FOUND_AUTHOR">
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

declare const g: <T, U, V>() => AsyncGenerator<T, U, V>;
>g : <T, U, V>() => AsyncGenerator<T, U, V>
> : ^ ^^ ^^ ^^^^^^^

async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookWithAuthor, unknown> {
>f : () => AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookWithAuthor, unknown>
> : ^^^^^^
Expand Down Expand Up @@ -91,6 +95,26 @@ async function* f(): AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", BookW
>mapper : <T>(result: Result<T, "NOT_FOUND_AUTHOR">) => Result<T, "NOT_FOUND_AUTHOR">
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

const x1 = yield* g();
>x1 : unknown
> : ^^^^^^^
>yield* g() : unknown
> : ^^^^^^^
>g() : AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", unknown, unknown>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>g : <T, U, V>() => AsyncGenerator<T, U, V>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

const x2: number = yield* g();
>x2 : number
> : ^^^^^^
>yield* g() : number
> : ^^^^^^
>g() : AsyncGenerator<"NOT_FOUND_AUTHOR" | "NOT_FOUND_BOOK", number, unknown>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>g : <T, U, V>() => AsyncGenerator<T, U, V>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

return null! as BookWithAuthor;
>null! as BookWithAuthor : BookWithAuthor
> : ^^^^^^^^^^^^^^
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/yieldStarContextualType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/compiler/yieldStarContextualType.ts] ////

=== yieldStarContextualType.ts ===
declare const g: <T, U, V>() => Generator<T, U, V>;
>g : Symbol(g, Decl(yieldStarContextualType.ts, 0, 13))
>T : Symbol(T, Decl(yieldStarContextualType.ts, 0, 18))
>U : Symbol(U, Decl(yieldStarContextualType.ts, 0, 20))
>V : Symbol(V, Decl(yieldStarContextualType.ts, 0, 23))
>Generator : Symbol(Generator, Decl(lib.es2015.generator.d.ts, --, --))
>T : Symbol(T, Decl(yieldStarContextualType.ts, 0, 18))
>U : Symbol(U, Decl(yieldStarContextualType.ts, 0, 20))
>V : Symbol(V, Decl(yieldStarContextualType.ts, 0, 23))

function* f(): Generator<string, void, unknown> {
>f : Symbol(f, Decl(yieldStarContextualType.ts, 0, 51))
>Generator : Symbol(Generator, Decl(lib.es2015.generator.d.ts, --, --))

const x1 = yield* g();
>x1 : Symbol(x1, Decl(yieldStarContextualType.ts, 3, 9))
>g : Symbol(g, Decl(yieldStarContextualType.ts, 0, 13))

const x2: number = yield* g();
>x2 : Symbol(x2, Decl(yieldStarContextualType.ts, 4, 9))
>g : Symbol(g, Decl(yieldStarContextualType.ts, 0, 13))
}
31 changes: 31 additions & 0 deletions tests/baselines/reference/yieldStarContextualType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/yieldStarContextualType.ts] ////

=== yieldStarContextualType.ts ===
declare const g: <T, U, V>() => Generator<T, U, V>;
>g : <T, U, V>() => Generator<T, U, V>
> : ^ ^^ ^^ ^^^^^^^

function* f(): Generator<string, void, unknown> {
>f : () => Generator<string, void, unknown>
> : ^^^^^^

const x1 = yield* g();
>x1 : unknown
> : ^^^^^^^
>yield* g() : unknown
> : ^^^^^^^
>g() : Generator<string, unknown, unknown>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>g : <T, U, V>() => Generator<T, U, V>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

const x2: number = yield* g();
>x2 : number
> : ^^^^^^
>yield* g() : number
> : ^^^^^^
>g() : Generator<string, number, unknown>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>g : <T, U, V>() => Generator<T, U, V>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
4 changes: 4 additions & 0 deletions tests/cases/compiler/asyncYieldStarContextualType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type BookWithAuthor = Book & { author: Author };

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

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

const x1 = yield* g();
const x2: number = yield* g();

return null! as BookWithAuthor;
}
9 changes: 9 additions & 0 deletions tests/cases/compiler/yieldStarContextualType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @target: esnext
// @noEmit: true

declare const g: <T, U, V>() => Generator<T, U, V>;

function* f(): Generator<string, void, unknown> {
const x1 = yield* g();
const x2: number = yield* g();
}

0 comments on commit d6bdb1a

Please sign in to comment.