Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6526,7 +6526,7 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e
iterationTypes := c.getIterationTypesOfIterableFast(t, c.asyncIterationTypesResolver)
if iterationTypes.hasTypes() {
if use&IterationUseForOfFlag != 0 {
return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode)
return c.getAsyncFromSyncIterationTypes(iterationTypes, use, errorNode)
}
return iterationTypes
}
Expand All @@ -6544,7 +6544,7 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e
iterationTypes := c.getIterationTypesOfIterableFast(t, c.syncIterationTypesResolver)
if iterationTypes.hasTypes() {
if use&IterationUseAllowsAsyncIterablesFlag != 0 {
return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode)
return c.getAsyncFromSyncIterationTypes(iterationTypes, use, errorNode)
}
return iterationTypes
}
Expand All @@ -6556,7 +6556,7 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e
}
}
if use&IterationUseAllowsAsyncIterablesFlag != 0 {
return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode)
return c.getAsyncFromSyncIterationTypes(iterationTypes, use, errorNode)
}
return iterationTypes
}
Expand Down Expand Up @@ -6653,7 +6653,7 @@ func (c *Checker) getIterationTypeUnion(iterationTypes []IterationTypes, f func(
return c.getUnionType(types)
}

func (c *Checker) getAsyncFromSyncIterationTypes(iterationTypes IterationTypes, errorNode *ast.Node) IterationTypes {
func (c *Checker) getAsyncFromSyncIterationTypes(iterationTypes IterationTypes, use IterationUse, errorNode *ast.Node) IterationTypes {
if !iterationTypes.hasTypes() ||
iterationTypes.yieldType == c.anyType && iterationTypes.returnType == c.anyType && iterationTypes.nextType == c.anyType {
return iterationTypes
Expand All @@ -6662,9 +6662,13 @@ func (c *Checker) getAsyncFromSyncIterationTypes(iterationTypes IterationTypes,
if errorNode != nil {
c.getGlobalAwaitedSymbol()
}
diagnostic := diagnostics.Type_of_await_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member
if use&IterationUseYieldStarFlag != 0 {
diagnostic = diagnostics.Type_of_iterated_elements_of_a_yield_Asterisk_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member
}
return IterationTypes{
yieldType: core.OrElse(c.getAwaitedTypeEx(iterationTypes.yieldType, errorNode, nil), c.anyType),
returnType: core.OrElse(c.getAwaitedTypeEx(iterationTypes.returnType, errorNode, nil), c.anyType),
yieldType: core.OrElse(c.getAwaitedTypeEx(iterationTypes.yieldType, errorNode, diagnostic), c.anyType),
returnType: core.OrElse(c.getAwaitedTypeEx(iterationTypes.returnType, errorNode, diagnostic), c.anyType),
nextType: iterationTypes.nextType,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
yieldStarSyncIterableThenable.ts(6,12): error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
yieldStarSyncIterableThenable.ts(10,11): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member.
yieldStarSyncIterableThenable.ts(14,31): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.


==== yieldStarSyncIterableThenable.ts (3 errors) ====
// https://github.com/microsoft/TypeScript/issues/65452

declare const thenable: { then(onfulfilled: number): void };

async function* delegated() {
yield* [thenable];
~~~~~~~~~~
!!! error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
}

async function* direct() {
yield thenable;
~~~~~~~~
!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member.
}

async function iterated() {
for await (const value of [thenable]) {
~~~~~~~~~~
!!! error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.
value;
}
}

async function* valid() {
yield* [1];
yield* [Promise.resolve(1)];
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [tests/cases/compiler/yieldStarSyncIterableThenable.ts] ////

//// [yieldStarSyncIterableThenable.ts]
// https://github.com/microsoft/TypeScript/issues/65452

declare const thenable: { then(onfulfilled: number): void };

async function* delegated() {
yield* [thenable];
}

async function* direct() {
yield thenable;
}

async function iterated() {
for await (const value of [thenable]) {
value;
}
}

async function* valid() {
yield* [1];
yield* [Promise.resolve(1)];
}


//// [yieldStarSyncIterableThenable.js]
"use strict";
// https://github.com/microsoft/TypeScript/issues/65452
async function* delegated() {
yield* [thenable];
}
async function* direct() {
yield thenable;
}
async function iterated() {
for await (const value of [thenable]) {
value;
}
}
async function* valid() {
yield* [1];
yield* [Promise.resolve(1)];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//// [tests/cases/compiler/yieldStarSyncIterableThenable.ts] ////

=== yieldStarSyncIterableThenable.ts ===
// https://github.com/microsoft/TypeScript/issues/65452

declare const thenable: { then(onfulfilled: number): void };
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableThenable.ts, 2, 13))
>then : Symbol(then, Decl(yieldStarSyncIterableThenable.ts, 2, 25))
>onfulfilled : Symbol(onfulfilled, Decl(yieldStarSyncIterableThenable.ts, 2, 31))

async function* delegated() {
>delegated : Symbol(delegated, Decl(yieldStarSyncIterableThenable.ts, 2, 60))

yield* [thenable];
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableThenable.ts, 2, 13))
}

async function* direct() {
>direct : Symbol(direct, Decl(yieldStarSyncIterableThenable.ts, 6, 1))

yield thenable;
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableThenable.ts, 2, 13))
}

async function iterated() {
>iterated : Symbol(iterated, Decl(yieldStarSyncIterableThenable.ts, 10, 1))

for await (const value of [thenable]) {
>value : Symbol(value, Decl(yieldStarSyncIterableThenable.ts, 13, 20))
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableThenable.ts, 2, 13))

value;
>value : Symbol(value, Decl(yieldStarSyncIterableThenable.ts, 13, 20))
}
}

async function* valid() {
>valid : Symbol(valid, Decl(yieldStarSyncIterableThenable.ts, 16, 1))

yield* [1];
yield* [Promise.resolve(1)];
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//// [tests/cases/compiler/yieldStarSyncIterableThenable.ts] ////

=== yieldStarSyncIterableThenable.ts ===
// https://github.com/microsoft/TypeScript/issues/65452

declare const thenable: { then(onfulfilled: number): void };
>thenable : { then(onfulfilled: number): void; }
>then : (onfulfilled: number) => void
>onfulfilled : number

async function* delegated() {
>delegated : () => AsyncGenerator<any, void, unknown>

yield* [thenable];
>yield* [thenable] : undefined
>[thenable] : { then(onfulfilled: number): void; }[]
>thenable : { then(onfulfilled: number): void; }
}

async function* direct() {
>direct : () => AsyncGenerator<never, void, unknown>

yield thenable;
>yield thenable : any
>thenable : { then(onfulfilled: number): void; }
}

async function iterated() {
>iterated : () => Promise<void>

for await (const value of [thenable]) {
>value : any
>[thenable] : { then(onfulfilled: number): void; }[]
>thenable : { then(onfulfilled: number): void; }

value;
>value : any
}
}

async function* valid() {
>valid : () => AsyncGenerator<number, void, unknown>

yield* [1];
>yield* [1] : undefined
>[1] : number[]
>1 : 1

yield* [Promise.resolve(1)];
>yield* [Promise.resolve(1)] : undefined
>[Promise.resolve(1)] : Promise<number>[]
>Promise.resolve(1) : Promise<number>
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
>1 : 1
}

25 changes: 25 additions & 0 deletions tsc/testdata/tests/cases/compiler/yieldStarSyncIterableThenable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @target: esnext
// @strict: true

// https://github.com/microsoft/TypeScript/issues/65452

declare const thenable: { then(onfulfilled: number): void };

async function* delegated() {
yield* [thenable];
}

async function* direct() {
yield thenable;
}

async function iterated() {
for await (const value of [thenable]) {
value;
}
}

async function* valid() {
yield* [1];
yield* [Promise.resolve(1)];
}
Loading